Repository navigation
Closed
Labels
BugA bug in TypeScriptDomain: Conditional TypesThe issue relates to conditional typesFixedA PR has been merged for this issue
Milestone
Description
TypeScript Version:
$ tsc --version
Version 3.3.0-dev.20181122
Search Terms:
unexpected, exclude
Code
/*
Some basic definitions of unions
*/
type Union = 'a' | 'b';
type Product<A extends Union, B> = { f1: A, f2: B};
type ProductUnion = Product<'a', 0> | Product<'b', 1>;
/*
These work as I would expect. Each element in the union is mapped
the complement or the double complement with nested Exclude
*/
type UnionComplement = {
[K in Union]: Exclude<Union, K>
};
// {a: "b"; b: "a"}
type UnionComplementComplement = {
[K in Union]: Exclude<Union, Exclude<Union, K>>
};
// {a: "a"; b: "b"}
/*
This also works as I would expect
*/
type ProductComplement = {
[K in Union]: Exclude<ProductUnion, { f1: K }>
};
// {a: Product<'b', 1>; b: Product<'a', 0>}
/*
Double complement on the other hand doesn't work
*/
type ProductComplementComplement = {
[K in Union]: Exclude<ProductUnion, Exclude<ProductUnion, { f1: K }>>
};
// {a: ProductUnion; b: ProductUnion}
/*
Explicit inlining works as I would expect
*/
type First = Exclude<ProductUnion, Exclude<ProductUnion, { f1: 'a' }>>;
// {f1: 'a'; f2: 0}
type Second = Exclude<ProductUnion, Exclude<ProductUnion, { f1: 'b' }>>;
// {f1: 'b'; f2: 1}
/*
Making parametrized types works as I would expect
*/
type Complementor<T> = {
[K in Union]: Exclude<T, { f1: K }>
};
type DoubleComplementor<T> = {
[K in Union]: Exclude<T, Exclude<T, { f1: K }>>
};
type Complement = Complementor<ProductUnion>;
// {a: Product<'b', 1>; b: Product<'a', 0>}
type DoubleComplement = DoubleComplementor<ProductUnion>;
// {a: Product<'a', 0>; b: Product<'b', 0>}Expected behavior:
I would expect the parametrized and non-parametrized type to work the same way but that's not
the case.
Actual behavior:
See the comments.
Playground Link: link
Related Issues: Some issues related to Pick and Exclude but didn't seem to apply.
Reacted by Joe Calzaretta
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptDomain: Conditional TypesThe issue relates to conditional typesFixedA PR has been merged for this issue
Activity
riggs commentedon Dec 3, 2018
I've run into a similar (likely related) issue, probably also having to do with distribution over
Exclude:My understanding is that
U & Functionwould expand intoExclude<any, Function> & Function, which would presumably benever.riggs commentedon Dec 3, 2018
From
lib.es5.d.ts:jack-williams commentedon Dec 4, 2018
I think this is a smaller repro:
If you hover over
Zit appears to have already resolved toRIGHT.The issue is that the conditional type is getting marked as non-deferred and it seems to be eagerly resolving using the constraint of
K. Related code here:The actual branch that is getting selected is later:
A candidate fix is:
but I think this is probably incomplete and there are other cases to deal with.
Wesley Wigham (@weswigham) I think you have an open PR that would fix this, right? (#27932)
riggs commentedon Dec 4, 2018
Pardon the side-track: Why wrap
Union&Kin a 1-tuple?jack-williams commentedon Dec 4, 2018
Essentially the compiler is trying to eagerly resolve the conditional type using wildcard types, substituting type parameters for
any. For example:K ==> any, or[K] ==> [any]. The compiler knows that any conditional type with a wildcardextendstype resolves to the wildcard type, but it misses the case when the wild card is wrapped in a 1-tuple.(I think).