Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions tsc/internal/checker/relater.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package checker

import (
"math/bits"
"slices"
"strconv"
"strings"
Expand Down Expand Up @@ -2869,10 +2870,29 @@ func (c *Checker) isTypeSubsetOf(source *Type, target *Type) bool {

func (c *Checker) isTypeSubsetOfUnion(source *Type, target *Type) bool {
if source.flags&TypeFlagsUnion != 0 {
for _, t := range source.Types() {
if !containsType(target.Types(), t) {
sourceTypes, targetTypes := source.Types(), target.Types()
if len(sourceTypes) > len(targetTypes) {
return false
}
// Scan by identity unless a few source types are looked for in a large target. A CompareTypes call costs
// roughly 8 pointer comparisons, so there a binary search is cheaper than walking the target.
if len(sourceTypes)*bits.Len(uint(len(targetTypes)))*8 >= len(targetTypes) {
for _, t := range sourceTypes {
i := slices.Index(targetTypes, t)
if i < 0 {
return false
}
// Union types are sorted by CompareTypes, so the next source type can only appear after this one.
targetTypes = targetTypes[i+1:]
}
return true
}
for _, t := range sourceTypes {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for _, t := range sourceTypes {
for _, t := range sourceTypes {

i, found := slices.BinarySearchFunc(targetTypes, t, CompareTypes)
if !found {
return false
}
targetTypes = targetTypes[i+1:]
}
return true
}
Expand Down
Loading