-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
Description
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:
propis a member of a specific union (here: the keys/property names oftarget) => the function returns a value of type 1 - Case B:
propis NOT a member of that union => the function returnsnull
Am I misunderstanding something obvious here or is what I'm trying to do simply not possible?
kpdonn and aneilbaboo
Metadata
Metadata
Assignees
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed