You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
identical conditional types not assignable to each other #21756
Identical conditional types are not assignable to each other.
TypeScript Version: 2.8.0-dev.20180208
Code
typeFoo<T>=Textendsstring ? boolean : number;typeBar<T>=Textendsstring ? boolean : number;constconvert=<T>(value: Foo<T>): Bar<T>=>value;// this failstypeBaz<T>=Foo<T>;constconvert2=<T>(value: Foo<T>): Baz<T>=>value;// this passes
Expected behavior:
no errors
Actual behavior:
TypeScript $ ./node_modules/.bin/tsc --noEmit test.ts
test.ts(4,47): error TS2322: Type 'Foo<T>' is not assignable to type 'Bar<T>'.
Type 'number | boolean' is not assignable to type 'Bar<T>'.
Type 'number' is not assignable to type 'Bar<T>'.
IMHO, if a conditional type is already resolved, compare the resolved type.
If target type is an unresolved conditional type, compare the source type:
if source type is not unresolved conditional type, it can be assigned to the target if the type can be both assigned to true type and false type,
if source type is unresolved conditional type, it can be assigned to the target if its branch types can be correspondingly assigned to target's branch types.
But this usage sounds weird, Tony Valderrama (@tvald) could you please give a concrete example where you need to assign/compare two conditional types?
It's reasonable to expect interoperability across compatible type definitions - perhaps originating from different libraries, perhaps narrowed from more complex types. Not supporting structural comparison of conditional types feels like a significant deviation from the way the rest of the type system works.
I agree, two conditional types should be identical if they have the same four constituent types and the same distributivity (i.e. both are distributive or both are not). Pretty easy to fix, we already have logic in place to intern conditional types, it just needs a bit of tweaking.
Activity
HerringtonDarkholme commentedon Feb 8, 2018
IMHO, if a conditional type is already resolved, compare the resolved type.
If target type is an unresolved conditional type, compare the source type:
But this usage sounds weird, Tony Valderrama (@tvald) could you please give a concrete example where you need to assign/compare two conditional types?
tvald commentedon Feb 8, 2018
This is the particular case:
It's reasonable to expect interoperability across compatible type definitions - perhaps originating from different libraries, perhaps narrowed from more complex types. Not supporting structural comparison of conditional types feels like a significant deviation from the way the rest of the type system works.
mhegazy commentedon Feb 8, 2018
This is behaving as intended, but i think we should reconsider.
ahejlsberg commentedon Feb 8, 2018
I agree, two conditional types should be identical if they have the same four constituent types and the same distributivity (i.e. both are distributive or both are not). Pretty easy to fix, we already have logic in place to intern conditional types, it just needs a bit of tweaking.