Skip to content

Narrowing discriminated union via type predicate and type assertion results in different behavior.Β #45770

@MicahZoltu

Description

@MicahZoltu

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).

Metadata

Metadata

Assignees

Labels

Needs InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions