TypeScript Version: 2.8.0-dev.20180320
Search Terms: conditional type assignable
Code
function foo<T1, T2 extends string>(target: T1, prop: T2): T2 extends keyof T1 ? true : false {
if (prop in target) return true;
return false; // doesn't matter if this line is here or not.
}
// or
function foo<T1, T2 extends string>(target: T1, prop: T2): T2 extends keyof T1 ? true : false {
return true;
}
// or
function foo<T1, T2 extends string>(target: T1, prop: T2): T2 extends keyof T1 ? true : null {
if (prop in target) return true;
}
Expected behavior:
It works
Actual behavior:
error TS2322: Type 'true' is not assignable to type 'T2 extends keyof T1 ? true : false'.
This repro is a very reduced version of some code I'm trying to type with the new conditional syntax. Basically I want to achieve the following:
- Case A:
prop is a member of a specific union (here: the keys/property names of target) => the function returns a value of type 1
- Case B:
prop is NOT a member of that union => the function returns null
Am I misunderstanding something obvious here or is what I'm trying to do simply not possible?
TypeScript Version: 2.8.0-dev.20180320
Search Terms: conditional type assignable
Code
Expected behavior:
It works
Actual behavior:
error TS2322: Type 'true' is not assignable to type 'T2 extends keyof T1 ? true : false'.This repro is a very reduced version of some code I'm trying to type with the new conditional syntax. Basically I want to achieve the following:
propis a member of a specific union (here: the keys/property names oftarget) => the function returns a value of type 1propis NOT a member of that union => the function returnsnullAm I misunderstanding something obvious here or is what I'm trying to do simply not possible?