Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
3451575
Infer type parameters from indexes on those parameters
weswigham Nov 18, 2017
f5b208c
Greatly simplify partial inference type instantiation through use of …
weswigham Nov 22, 2017
ba064ac
Add many more tests showing current behaviors
weswigham Nov 22, 2017
93afc10
Discriminate partial inferences if not complete enough to satisfy con…
weswigham Dec 1, 2017
49e1961
Move case to prefered location
weswigham Dec 21, 2017
96772e5
Small refactor to reduce how many comparisons are performed
weswigham Dec 21, 2017
2e0c635
Infer reverse mapped types based on inferences made for its concrete …
Andarist Feb 28, 2023
7b8cf10
Merge remote-tracking branch 'origin/main' into infer-concrete-proper…
Andarist Apr 17, 2023
e7e4e70
Merge remote-tracking branch 'weswigham/index-combined-inferences' in…
Andarist Apr 17, 2023
d0e8b8b
update baselines, note: some are broken
Andarist Apr 17, 2023
e5d0ea8
use new `inference.indexes` in reverse mapped type inference
Andarist Apr 17, 2023
b0b2fcb
update reverse mapped baseline
Andarist Apr 18, 2023
7fe118a
Call `getActualTypeVariable` appropriately to fix inference for a tup…
Andarist Apr 18, 2023
50e6a0c
Avoid inferring an index under `InferencePriority.NakedTypeVariable`
Andarist Apr 18, 2023
4febb9c
always discard aggregate inference that is not assignable to the cons…
Andarist Apr 19, 2023
c2d4b0f
bring back the union discriminating logic
Andarist Apr 19, 2023
11b02d0
fixed incorrect indexed access in tests
Andarist Apr 19, 2023
97adac5
only use the aggregate inference when there are no other candidates
Andarist Apr 19, 2023
8ffcb94
do not collect index-based inferences from non-inferrable types
Andarist Apr 20, 2023
192d8b7
Merge remote-tracking branch 'origin/main' into infer-concrete-proper…
Andarist May 22, 2023
5fa585c
Merge remote-tracking branch 'origin/main' into infer-concrete-proper…
Andarist Jun 13, 2023
375c127
Merge remote-tracking branch 'origin/main' into infer-concrete-proper…
Andarist Aug 22, 2023
a62749c
Merge remote-tracking branch 'origin/main' into infer-concrete-proper…
Andarist Sep 15, 2024
2f86371
Merge remote-tracking branch 'origin/main' into infer-concrete-proper…
Andarist Sep 16, 2024
9825bcd
update baselines
Andarist Sep 16, 2024
46e280b
Merge remote-tracking branch 'origin/main' into infer-concrete-proper…
Andarist Nov 28, 2025
c90fb20
instantiate aggregate inference
Andarist Nov 30, 2025
2b580c8
experiment with `eraseSelfMapper`
Andarist Dec 1, 2025
a390703
experiment further
Andarist Dec 1, 2025
15eda09
drop unused code
Andarist Dec 1, 2025
e3f6b43
discard never aggregate inferences
Andarist Dec 2, 2025
1911406
prefer default
Andarist Dec 2, 2025
20d6feb
Merge remote-tracking branch 'origin/main' into infer-concrete-proper…
Andarist Dec 2, 2025
a554bbf
change condition for indexes gathering
Andarist Dec 2, 2025
22ddecc
simplify test
Andarist Dec 2, 2025
ced3cc1
refactor
Andarist Dec 2, 2025
a09f410
remove `PartialInference`
Andarist Dec 3, 2025
7bc77f1
add tests
Andarist Dec 3, 2025
097b8e6
Merge remote-tracking branch 'origin/main' into infer-concrete-proper…
Andarist Dec 5, 2025
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
Prev Previous commit
Next Next commit
discard never aggregate inferences
  • Loading branch information
Andarist committed Dec 2, 2025
commit e3f6b434ca00a1aac901b085e91cd9d14931faee
17 changes: 12 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26447,7 +26447,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
if (inference.indexes) {
const eraseSelfMapper = makeArrayTypeMapper([constraint.type, typeParameter], [allKeysAllKeysUnknownType, stringNumberSymbolType]);
return instantiateType(getIntersectionType(inference.indexes), eraseSelfMapper);
const aggregateInference = instantiateType(getIntersectionType(inference.indexes), eraseSelfMapper);
if (!(getReducedType(aggregateInference).flags & TypeFlags.Never)) {
return aggregateInference;
}
}
return unknownType;
}
Expand Down Expand Up @@ -26900,8 +26903,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return;
}
if (!inference.isFixed) {
// Instantiates instance of `type PartialInference<T, Keys extends string> = ({[K in Keys]: {[K1 in K]: T}})[Keys];`
// Where `T` is `source` and `Keys` is `target.indexType`
const partialInferenceTypeSymbol = getGlobalPartialInferenceSymbol();
if (partialInferenceTypeSymbol) {
if ((target as IndexedAccessType).indexType.flags & TypeFlags.Instantiable) {
Expand All @@ -26911,6 +26912,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}
else {
// Instantiates instance of `type PartialInference<T, Keys extends PropertyKey> = ({[K in Keys]: {[K1 in K]: T}})[Keys];`
// Where `T` is `source` and `Keys` is `target.indexType`
inference.indexes = append(inference.indexes, getTypeAliasInstantiation(partialInferenceTypeSymbol, [source, (target as IndexedAccessType).indexType]));
}
}
Expand Down Expand Up @@ -27591,9 +27594,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
else if (inference.indexes) {
const eraseSelfMapper = makeUnaryTypeMapper(inference.typeParameter, allKeysUnknownType);
let aggregateInference = instantiateType(getIntersectionType(inference.indexes), mergeTypeMappers(eraseSelfMapper, context.nonFixingMapper));
let aggregateInference: Type | undefined = instantiateType(getIntersectionType(inference.indexes), mergeTypeMappers(eraseSelfMapper, context.nonFixingMapper));
if (getReducedType(aggregateInference).flags & TypeFlags.Never) {
// `never` inference isn't that useful of an inference given its assignable to every other type
aggregateInference = undefined;
}
const constraint = getConstraintOfTypeParameter(inference.typeParameter);
if (constraint) {
if (aggregateInference && constraint) {
const instantiatedConstraint = instantiateType(constraint, context.nonFixingMapper);
if (instantiatedConstraint.flags & TypeFlags.Union && !context.compareTypes(aggregateInference, getTypeWithThisArgument(instantiatedConstraint, aggregateInference))) {
const discriminantProps = findDiscriminantProperties(getPropertiesOfType(aggregateInference), instantiatedConstraint);
Expand Down
138 changes: 138 additions & 0 deletions tests/baselines/reference/indexAccessCombinedInference2.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
//// [tests/cases/compiler/indexAccessCombinedInference2.ts] ////

=== indexAccessCombinedInference2.ts ===
type TLArrowShape = {
>TLArrowShape : Symbol(TLArrowShape, Decl(indexAccessCombinedInference2.ts, 0, 0))

type: "arrow";
>type : Symbol(type, Decl(indexAccessCombinedInference2.ts, 0, 21))

id: string;
>id : Symbol(id, Decl(indexAccessCombinedInference2.ts, 1, 16))

x: number;
>x : Symbol(x, Decl(indexAccessCombinedInference2.ts, 2, 13))

y: number;
>y : Symbol(y, Decl(indexAccessCombinedInference2.ts, 3, 12))

props: {
>props : Symbol(props, Decl(indexAccessCombinedInference2.ts, 4, 12))

dir: 1 | -1;
>dir : Symbol(dir, Decl(indexAccessCombinedInference2.ts, 5, 10))

};
};

type NodeShape = {
>NodeShape : Symbol(NodeShape, Decl(indexAccessCombinedInference2.ts, 8, 2))

type: "node";
>type : Symbol(type, Decl(indexAccessCombinedInference2.ts, 10, 18))

id: string;
>id : Symbol(id, Decl(indexAccessCombinedInference2.ts, 11, 15))

x: number;
>x : Symbol(x, Decl(indexAccessCombinedInference2.ts, 12, 13))

y: number;
>y : Symbol(y, Decl(indexAccessCombinedInference2.ts, 13, 12))

props: {
>props : Symbol(props, Decl(indexAccessCombinedInference2.ts, 14, 12))

nodeType: string;
>nodeType : Symbol(nodeType, Decl(indexAccessCombinedInference2.ts, 15, 10))

};
};

type TLShape = TLArrowShape | NodeShape;
>TLShape : Symbol(TLShape, Decl(indexAccessCombinedInference2.ts, 18, 2))
>TLArrowShape : Symbol(TLArrowShape, Decl(indexAccessCombinedInference2.ts, 0, 0))
>NodeShape : Symbol(NodeShape, Decl(indexAccessCombinedInference2.ts, 8, 2))

export type TLShapePartial<T extends TLShape = TLShape> = T extends T
>TLShapePartial : Symbol(TLShapePartial, Decl(indexAccessCombinedInference2.ts, 20, 40))
>T : Symbol(T, Decl(indexAccessCombinedInference2.ts, 22, 27))
>TLShape : Symbol(TLShape, Decl(indexAccessCombinedInference2.ts, 18, 2))
>TLShape : Symbol(TLShape, Decl(indexAccessCombinedInference2.ts, 18, 2))
>T : Symbol(T, Decl(indexAccessCombinedInference2.ts, 22, 27))
>T : Symbol(T, Decl(indexAccessCombinedInference2.ts, 22, 27))

? {
id: string;
>id : Symbol(id, Decl(indexAccessCombinedInference2.ts, 23, 5))

type: T["type"];
>type : Symbol(type, Decl(indexAccessCombinedInference2.ts, 24, 17))
>T : Symbol(T, Decl(indexAccessCombinedInference2.ts, 22, 27))

props?: Partial<T["props"]>;
>props : Symbol(props, Decl(indexAccessCombinedInference2.ts, 25, 22))
>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --))
>T : Symbol(T, Decl(indexAccessCombinedInference2.ts, 22, 27))

} & Partial<Omit<T, "type" | "id" | "props">>
>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --))
>Omit : Symbol(Omit, Decl(lib.es5.d.ts, --, --))
>T : Symbol(T, Decl(indexAccessCombinedInference2.ts, 22, 27))

: never;

declare class Editor {
>Editor : Symbol(Editor, Decl(indexAccessCombinedInference2.ts, 28, 10))

updateShape<T extends TLShape = TLShape>(
>updateShape : Symbol(Editor.updateShape, Decl(indexAccessCombinedInference2.ts, 30, 22))
>T : Symbol(T, Decl(indexAccessCombinedInference2.ts, 31, 14))
>TLShape : Symbol(TLShape, Decl(indexAccessCombinedInference2.ts, 18, 2))
>TLShape : Symbol(TLShape, Decl(indexAccessCombinedInference2.ts, 18, 2))

partial: TLShapePartial<T> | null | undefined,
>partial : Symbol(partial, Decl(indexAccessCombinedInference2.ts, 31, 43))
>TLShapePartial : Symbol(TLShapePartial, Decl(indexAccessCombinedInference2.ts, 20, 40))
>T : Symbol(T, Decl(indexAccessCombinedInference2.ts, 31, 14))

): T;
>T : Symbol(T, Decl(indexAccessCombinedInference2.ts, 31, 14))
}

declare const x: number;
>x : Symbol(x, Decl(indexAccessCombinedInference2.ts, 36, 13))

declare const y: number;
>y : Symbol(y, Decl(indexAccessCombinedInference2.ts, 37, 13))

declare const editor: Editor;
>editor : Symbol(editor, Decl(indexAccessCombinedInference2.ts, 38, 13))
>Editor : Symbol(Editor, Decl(indexAccessCombinedInference2.ts, 28, 10))

declare const node1: NodeShape;
>node1 : Symbol(node1, Decl(indexAccessCombinedInference2.ts, 40, 13))
>NodeShape : Symbol(NodeShape, Decl(indexAccessCombinedInference2.ts, 8, 2))

const node2 = editor.updateShape({ ...node1, x, y });
>node2 : Symbol(node2, Decl(indexAccessCombinedInference2.ts, 41, 5))
>editor.updateShape : Symbol(Editor.updateShape, Decl(indexAccessCombinedInference2.ts, 30, 22))
>editor : Symbol(editor, Decl(indexAccessCombinedInference2.ts, 38, 13))
>updateShape : Symbol(Editor.updateShape, Decl(indexAccessCombinedInference2.ts, 30, 22))
>node1 : Symbol(node1, Decl(indexAccessCombinedInference2.ts, 40, 13))
>x : Symbol(x, Decl(indexAccessCombinedInference2.ts, 41, 44))
>y : Symbol(y, Decl(indexAccessCombinedInference2.ts, 41, 47))

declare const shape1: TLShape;
>shape1 : Symbol(shape1, Decl(indexAccessCombinedInference2.ts, 43, 13))
>TLShape : Symbol(TLShape, Decl(indexAccessCombinedInference2.ts, 18, 2))

const shape2 = editor.updateShape({ ...shape1, x, y });
>shape2 : Symbol(shape2, Decl(indexAccessCombinedInference2.ts, 44, 5))
>editor.updateShape : Symbol(Editor.updateShape, Decl(indexAccessCombinedInference2.ts, 30, 22))
>editor : Symbol(editor, Decl(indexAccessCombinedInference2.ts, 38, 13))
>updateShape : Symbol(Editor.updateShape, Decl(indexAccessCombinedInference2.ts, 30, 22))
>shape1 : Symbol(shape1, Decl(indexAccessCombinedInference2.ts, 43, 13))
>x : Symbol(x, Decl(indexAccessCombinedInference2.ts, 44, 46))
>y : Symbol(y, Decl(indexAccessCombinedInference2.ts, 44, 49))

168 changes: 168 additions & 0 deletions tests/baselines/reference/indexAccessCombinedInference2.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
//// [tests/cases/compiler/indexAccessCombinedInference2.ts] ////

=== indexAccessCombinedInference2.ts ===
type TLArrowShape = {
>TLArrowShape : TLArrowShape
> : ^^^^^^^^^^^^

type: "arrow";
>type : "arrow"
> : ^^^^^^^

id: string;
>id : string
> : ^^^^^^

x: number;
>x : number
> : ^^^^^^

y: number;
>y : number
> : ^^^^^^

props: {
>props : { dir: 1 | -1; }
> : ^^^^^^^ ^^^

dir: 1 | -1;
>dir : 1 | -1
> : ^^^^^^
>-1 : -1
> : ^^
>1 : 1
> : ^

};
};

type NodeShape = {
>NodeShape : NodeShape
> : ^^^^^^^^^

type: "node";
>type : "node"
> : ^^^^^^

id: string;
>id : string
> : ^^^^^^

x: number;
>x : number
> : ^^^^^^

y: number;
>y : number
> : ^^^^^^

props: {
>props : { nodeType: string; }
> : ^^^^^^^^^^^^ ^^^

nodeType: string;
>nodeType : string
> : ^^^^^^

};
};

type TLShape = TLArrowShape | NodeShape;
>TLShape : TLShape
> : ^^^^^^^

export type TLShapePartial<T extends TLShape = TLShape> = T extends T
>TLShapePartial : TLShapePartial<T>
> : ^^^^^^^^^^^^^^^^^

? {
id: string;
>id : string
> : ^^^^^^

type: T["type"];
>type : T["type"]
> : ^^^^^^^^^

props?: Partial<T["props"]>;
>props : Partial<T["props"]> | undefined
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

} & Partial<Omit<T, "type" | "id" | "props">>
: never;

declare class Editor {
>Editor : Editor
> : ^^^^^^

updateShape<T extends TLShape = TLShape>(
>updateShape : <T extends TLShape = TLShape>(partial: TLShapePartial<T> | null | undefined) => T
> : ^ ^^^^^^^^^ ^^^^^^^^^^^^ ^^ ^^^^^

partial: TLShapePartial<T> | null | undefined,
>partial : TLShapePartial<T> | null | undefined
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

): T;
}

declare const x: number;
>x : number
> : ^^^^^^

declare const y: number;
>y : number
> : ^^^^^^

declare const editor: Editor;
>editor : Editor
> : ^^^^^^

declare const node1: NodeShape;
>node1 : NodeShape
> : ^^^^^^^^^

const node2 = editor.updateShape({ ...node1, x, y });
>node2 : { type: "node"; x: number; y: number; id: string; props: { nodeType: string; }; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^ ^^^
>editor.updateShape({ ...node1, x, y }) : { type: "node"; x: number; y: number; id: string; props: { nodeType: string; }; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^ ^^^
>editor.updateShape : <T extends TLShape = TLShape>(partial: TLShapePartial<T> | null | undefined) => T
> : ^ ^^^^^^^^^ ^^^^^^^^^^^^ ^^ ^^^^^
>editor : Editor
> : ^^^^^^
>updateShape : <T extends TLShape = TLShape>(partial: TLShapePartial<T> | null | undefined) => T
> : ^ ^^^^^^^^^ ^^^^^^^^^^^^ ^^ ^^^^^
>{ ...node1, x, y } : { x: number; y: number; type: "node"; id: string; props: { nodeType: string; }; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^ ^^^^^^^^^ ^^^
>node1 : NodeShape
> : ^^^^^^^^^
>x : number
> : ^^^^^^
>y : number
> : ^^^^^^

declare const shape1: TLShape;
>shape1 : TLShape
> : ^^^^^^^

const shape2 = editor.updateShape({ ...shape1, x, y });
>shape2 : TLShape
> : ^^^^^^^
>editor.updateShape({ ...shape1, x, y }) : TLShape
> : ^^^^^^^
>editor.updateShape : <T extends TLShape = TLShape>(partial: TLShapePartial<T> | null | undefined) => T
> : ^ ^^^^^^^^^ ^^^^^^^^^^^^ ^^ ^^^^^
>editor : Editor
> : ^^^^^^
>updateShape : <T extends TLShape = TLShape>(partial: TLShapePartial<T> | null | undefined) => T
> : ^ ^^^^^^^^^ ^^^^^^^^^^^^ ^^ ^^^^^
>{ ...shape1, x, y } : { x: number; y: number; type: "arrow"; id: string; props: { dir: 1 | -1; }; } | { x: number; y: number; type: "node"; id: string; props: { nodeType: string; }; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^ ^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^ ^^^^^^^^^ ^^^
>shape1 : TLShape
> : ^^^^^^^
>x : number
> : ^^^^^^
>y : number
> : ^^^^^^

Loading