Closed
Description
TypeScript Version: 3.6.3
Search Terms: user type guard function nested guarding
Code
type Test = TestOne | TestTwo;
interface TestOne {
type: 'one'
}
interface TestTwo {
type: 'two'
}
const isTest = (data: unknown): data is Test => true
declare var payload: any
if (isTest(payload)) {
if (payload.type === 'two') {
// expected to be `TestTwo`, doesn't work
payload
}
}
const castedPayload = payload as Test
if (castedPayload.type === 'two') {
// expected to be `TestTwo`, suddenly works
castedPayload
}
Expected behavior:
Test
should be type guarded and become TestTwo
inside the if
statement.
Actual behavior:
Test
is not type guarded after the check. It remains Test
.
Strangely enough, if payload
is manually cast with as
to be Test
, type guarding suddenly works.
So, it seems to be tighed some how to the user type guard function isTest
.
Playground Link: link
Related Issues:
Similar to the #26729, but not the same
Metadata
Metadata
Assignees
Labels
No labels