Closed
Description
🔎 Search Terms
site:github.com/microsoft/TypeScript Exclude
site:github.com/microsoft/TypeScript Exclude behaves differently
TypeScript Exclude inconsistent
TypeScript Exclude different
TypeScript Exclude behaves differently expanded
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about "Exclude" and "Distributive Conditional Types"
⏯ Playground Link
💻 Code
type _Exclude<T, U> = T extends U ? never : T;
type MyType1 = 1 | 2 | 3 | 4
type MyType2 = 3 | 4 | 5 | 6
type Result1 = _Exclude<MyType1, MyType2> // 1 | 2
type Result2 = MyType1 extends MyType2 ? never : MyType1 // 1 | 2 | 3 | 4
type Result3 = [MyType1] extends MyType2 ? never : MyType1 // 1 | 2 | 3 | 4
// Maybe an improvement for non-generic cases?
type Result4 = [K in MyType1] extends MyType2 ? never : K // ERROR
🙁 Actual behavior
Using "Distributive Conditional Types" in a generic type behaves differently compared to using concrete types. (Not Referential transparent)
🙂 Expected behavior
"Distributive Conditional Types" always works the same, no matter if it is being used in generic types or not.
Additional information about the issue
If "Distributive Conditional Types" should behave differently, I believe it worth providing a way for non-generic types to be able to distribute over the union type in conditional types, same as how it works in generics. Maybe having a syntax like:
type MyType1 = 1 | 2 | 3 | 4
type MyType2 = 3 | 4 | 5 | 6
type Result4 = [K in MyType1] extends MyType2 ? never : K