-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Using tagged unions to narrow generics #18215
Comments
This has nothing to do with union types, here is simplified version interface TypeA {
type: 'a';
foo: number;
}
function make<T extends TypeA>(): T {
return {
type: 'a',
foo: 1
}
} Error: Type '{ type: "a"; foo: number; }' is not assignable to type 'T'. Because the parameter T may be a subtype of TypeA. Try using instead this signature function make<T extends BothTypes>(type: T['type']): BothTypes |
Probably the desired workaround looks more like: type MapTypes = {
a: TypeA;
b: TypeB;
}
function make<T extends BothTypes['type']>(type: T): MapTypes[T] {
const t: BothTypes['type'] = type; // help control flow analysis
switch (t) {
case 'a':
return {
type: 'a',
foo: 1
};
case 'b':
return {
type: 'b',
bar: 1
};
}
const never: never = t; // exhaustiveness check
return never;
}
const obj = make('a');
console.log(obj.foo); // ok |
seems like another manifestation of #17859. |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
When using a tagged union as type argument to a generic function it would be nice if you can use the discriminant to narrow down the generic type to a single piece of the union.
Example:
currently typescript complains that both return statements are not assignable to
T
, and the returned object is the genericBothTypes
instead ofTypeA
.The text was updated successfully, but these errors were encountered: