-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Open
Labels
Needs InvestigationThis issue needs a team member to investigate its status.This issue needs a team member to investigate its status.
Milestone
Description
Bug Report
π Search Terms
type predicate discriminated union
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about v4.5.0-dev.20210907
β― Playground Link
Playground link with relevant code
π» Code
declare const fruit: { kind: 'apple'} | { kind: 'banana' } | { kind: 'cherry' }
declare function isOneOf<T, U extends T>(item: T, array: readonly U[]): item is U
if (isOneOf(fruit.kind, ['apple', 'banana'] as const)) {
fruit.kind // 'apple' | 'banana'
fruit // { kind: 'apple'} | { kind: 'banana' } | { kind: 'cherry' }
} else {
fruit.kind // 'cherry'
fruit // { kind: 'apple'} | { kind: 'banana' } | { kind: 'cherry' }
}
declare function isDefinitelyOneOf<T, U extends T>(item: T, array: readonly U[]): asserts item is U
isDefinitelyOneOf(fruit.kind, ['apple', 'banana'] as const)
fruit.kind // 'apple' | 'banana'
fruit // { kind: 'apple'} | { kind: 'banana' }
π Actual behavior
Type assertion does the correct thing, but the equivalent type predicate results in an odd scenario where the property is correctly narrowed, but the union object is not narrowed.
π Expected behavior
Type assertion and type predicate have the same narrowing behavior, and only differ on whether the code branches or throws.
I would also expect the type predicate to either narrow neither the property nor the object or both the property and the object (like the type assertion does).
nickrum, iamogbz and DaviDevMod
Metadata
Metadata
Assignees
Labels
Needs InvestigationThis issue needs a team member to investigate its status.This issue needs a team member to investigate its status.