Partial Discriminated Union cannot always be narrowed
Closed this issue ยท 3 comments
๐ Search Terms
narrow partial discriminated union
๐ Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about union types
โฏ Playground Link
๐ป Code
enum Types {
Foo = 'foo',
Bar = 'bar',
}
interface FooParameters {
applicationType: Types.Foo;
foo: number;
}
interface BarParameters {
applicationType: Types.Bar;
bar: number;
}
type Params = FooParameters | BarParameters;
function test_non_partial_does_narrow(p: Params) {
if (p.applicationType === undefined || p.applicationType === Types.Foo) {
return;
}
console.log(p.bar);
// ^? BarParameters
}
function test_partial_does_not_narrow(p: Partial<Params>) {
if (p.applicationType === undefined || p.applicationType === Types.Foo) {
return;
}
console.log(p.bar);
// ^? Partial<Params>
}
function test_partial_does_narrow(p: Partial<Params>) {
if (p.applicationType !== Types.Bar) {
return;
}
console.log(p.bar);
// ^? Partial<BarParameters>
}๐ Actual behavior
The Partial<Params> discriminated union can only be narrowed if you return when it does not equal the expected value. When ruling out all other possibilities it does not narrow the type.
๐ Expected behavior
It also narrows the type, when ruling out all other possibilities
Additional information about the issue
No response
I believe this is a duplicate of #31404.
๐ค Thank you for your issue! I've done some analysis to help get you started. This response is automatically generated; feel free to ๐ or ๐ this comment according to its usefulness.
Similar Issues
Here are the most similar issues I found
- (77%) microsoft/typescript#55578: Incorrect narrowing on partial of discriminated union
- (72%) microsoft/typescript#11989: Discriminated Unions Incompatible with Intersection Types
- (71%) microsoft/typescript#55036: Discriminated union containing optional properties and generic types is not narrowed
- (71%) microsoft/typescript#55425: Discriminated union narrowing breaks when discriminating from a wider union type
- (71%) microsoft/typescript#59151: discriminated union using non-literal-type
- (70%) microsoft/typescript#31404: Narrowing discriminated unions with union discriminant properties
- (70%) microsoft/typescript#54369: Discriminated union case with a union of discriminators not correctly excluded
- (70%) microsoft/typescript#61202: Union type on object's property behaves different than having a union of the whole object with all possible values for this field
- (70%) microsoft/typescript#48522: Narrowing not works on discriminated unions when not using literal type as discriminator property
- (70%) microsoft/typescript#49782: User-defined type guards cannot narrow discriminated union types
- (70%) microsoft/typescript#56023: Composite types fails to type narrowing when it contains two or more possibilities to a narrowable property
- (69%) microsoft/typescript#39110: Discriminant narrow not work when not union type.
- (69%) microsoft/typescript#49334: Strange narrowing behavior on objects using discriminator property
- (69%) microsoft/typescript#31045: Union type inference doesn't distinguish types with constant values if any of them has a non-constant value
- (69%) microsoft/typescript#40035: Discriminated union narrowed incorrectly by user-defined type predicate
If your issue is a duplicate of one of these, feel free to close this issue. Otherwise, no action is needed.
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.