Skip to content

Commit 5a757e9

Browse files
Cache the constraint of generic indexed access types (#65664)
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 298282d commit 5a757e9

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

‎tsc/internal/checker/checker.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17727,6 +17727,19 @@ func (c *Checker) getConstraintOfIndexedAccess(t *Type) *Type {
1772717727
}
1772817728

1772917729
func (c *Checker) getConstraintFromIndexedAccess(t *Type) *Type {
17730+
d := t.AsIndexedAccessType()
17731+
if d.resolvedConstraint == nil {
17732+
// The constraint is cached because it can be expensive to compute (e.g. when the index type is constrained
17733+
// to a large union) and type relations may request it many times, such as once per constituent of a target union.
17734+
d.resolvedConstraint = core.OrElse(c.computeConstraintFromIndexedAccess(t), c.noConstraintType)
17735+
}
17736+
if d.resolvedConstraint != c.noConstraintType {
17737+
return d.resolvedConstraint
17738+
}
17739+
return nil
17740+
}
17741+
17742+
func (c *Checker) computeConstraintFromIndexedAccess(t *Type) *Type {
1773017743
d := t.AsIndexedAccessType()
1773117744
if c.isMappedTypeGenericIndexedAccess(t) {
1773217745
// For indexed access types of the form { [P in K]: E }[X], where K is non-generic and X is generic,

‎tsc/internal/checker/types.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,9 +1539,10 @@ func (t *IndexType) Target() *Type { return t.target }
15391539

15401540
type IndexedAccessType struct {
15411541
ConstrainedType
1542-
objectType *Type
1543-
indexType *Type
1544-
accessFlags AccessFlags // Only includes AccessFlags.Persistent
1542+
objectType *Type
1543+
indexType *Type
1544+
accessFlags AccessFlags // Only includes AccessFlags.Persistent
1545+
resolvedConstraint *Type
15451546
}
15461547

15471548
func (t *IndexedAccessType) ObjectType() *Type { return t.objectType }
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// @strict: true
2+
// @noEmit: true
3+
// @noTypesAndSymbols: true
4+
5+
// https://github.com/microsoft/TypeScript/issues/65654
6+
7+
type Digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
8+
type Table = `t${Digit}${Digit}`; // 100 tables
9+
type TableColumn<T extends Table> = "id" | "name" | `${T}_c${Digit}`; // 12 columns per table
10+
11+
type ColumnMap = { [T in Table]: TableColumn<T> };
12+
interface Columns extends ColumnMap {}
13+
type Column = TableColumn<Table>;
14+
type ColumnFor<T extends Table> = Columns[T];
15+
16+
declare function parent<C extends Column | undefined>(column?: C): C;
17+
18+
export function parentOf<T extends Table>(column: ColumnFor<T>) {
19+
return parent(column);
20+
}

0 commit comments

Comments
 (0)