Skip to content

Commit e280a8c

Browse files
authored
Port "Improve unknown narrowing by negated type predicates" (#1044)
1 parent 9b963fd commit e280a8c

File tree

3 files changed

+7
-24
lines changed

3 files changed

+7
-24
lines changed

internal/checker/flow.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -841,10 +841,13 @@ func (c *Checker) getNarrowedTypeWorker(t *Type, candidate *Type, assumeTrue boo
841841
return !c.isTypeDerivedFrom(t, candidate)
842842
})
843843
}
844+
if t.flags&TypeFlagsUnknown != 0 {
845+
t = c.unknownUnionType
846+
}
844847
trueType := c.getNarrowedType(t, candidate, true /*assumeTrue*/, false /*checkDerived*/)
845-
return c.filterType(t, func(t *Type) bool {
848+
return c.recombineUnknownType(c.filterType(t, func(t *Type) bool {
846849
return !c.isTypeSubsetOf(t, trueType)
847-
})
850+
}))
848851
}
849852
if t.flags&TypeFlagsAnyOrUnknown != 0 {
850853
return candidate

testdata/baselines/reference/submodule/compiler/narrowUnknownByTypePredicate.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ if (!isNotNullish(value2)) {
3131
>value2 : unknown
3232

3333
value2;
34-
>value2 : unknown
34+
>value2 : null | undefined
3535
}
3636

3737
declare const value3: unknown;
@@ -56,7 +56,7 @@ if (!isNullish(value4)) {
5656
>value4 : unknown
5757

5858
value4;
59-
>value4 : unknown
59+
>value4 : {}
6060
}
6161

6262
declare class A { foo: string; }

testdata/baselines/reference/submodule/compiler/narrowUnknownByTypePredicate.types.diff

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)