-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
Bug Report
π Search Terms
union type, type predicate
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about "Common Bugs That Aren't Bugs"
β― Playground Link
Playground link with relevant code
π» Code
type Foo = {kind: 'foo'}
interface Bar<T = Any> {kind: 'bar', value: T}
type Baz = {kind: 'baz'}
type Any = Foo | Bar | Baz
function isFoobar(value: unknown): value is Foo | Bar<Foo> {
return typeof value === 'object' && ((value as any)?.kind === 'foo' || (value as any)?.kind === 'bar')
}
let value = {kind: 'baz'} as Any
if (isFoobar(value)) {
let a = value.kind === 'foo'
? value.kind
: value.value.kind
}
π Actual behavior & π Expected behavior
isFoobar
narrows the type to only Foo
but should narrow it to the specified Foo | Bar<Foo>
. It seems to be related to the combination of the generic parameter and the circularity because I can not find a way to simplify it further.
andrewbranch
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created