-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type 'true' is not assignable to type 'T2 extends keyof T1 ? true : false' #22735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Likely duplicate of #22596 |
In order for this to work you have to explicitly cast the return values to match the return type annotation because the type checker isn't capable of proving that it is always true: function foo<T1, T2 extends string>(target: T1, prop: T2): T2 extends keyof T1 ? true : false {
if (prop in target) return <T2 extends keyof T1 ? true : false>true;
return <T2 extends keyof T1 ? true : false>false;
} But I'm wondering why you're using a conditional type in the return type annotation (as opposed to just @RyanCavanaugh This is unrelated to #22596. |
This is just a simplified repro which happened to result in the same error. I actually want to type something slightly more complicated:
I guess the error happens because (like you said) the type checker is not sure if the condition is fulfilled. However I'd expect that returning one of the possible outcomes of the conditional type should be possible. What about this case? If I type it using function ensureString<T>(bar: T): T extends string ? string : null {
if (typeof bar === "string") return bar as string;
// ^ ERROR: string is not assignable to T extends string ? string : null
return;
}
var x = ensureString(""); // correctly inferred as string
var y = ensureString(1); // correctly inferred as null |
Sorry for bringing this up again... but is the behavior I described in my last post intended? |
it is expected given the current design. but it is more of a design limitation than a desired behavior. The type of in a sense what you need the compiler to do here is 1. narrow the type of you can cast your way out of this using a conditional type using if (typeof bar === "string") return <T extends string ? T : never> bar; |
Alright, thanks for the explanation! |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
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:
prop
is a member of a specific union (here: the keys/property names oftarget
) => the function returns a value of type 1prop
is 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?
The text was updated successfully, but these errors were encountered: