Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer type parameters from indexes on those parameters #53017

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 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
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
58 changes: 57 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25729,6 +25729,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
topLevel: true,
isFixed: false,
impliedArity: undefined,
indexes: undefined,
};
}

Expand All @@ -25742,6 +25743,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
topLevel: inference.topLevel,
isFixed: inference.isFixed,
impliedArity: inference.impliedArity,
indexes: inference.indexes && inference.indexes.slice(),
};
}

Expand Down Expand Up @@ -25894,7 +25896,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const templateType = getTemplateTypeFromMappedType(target);
const inference = createInferenceInfo(typeParameter);
inferTypes([inference], sourceType, templateType);
return getTypeFromInference(inference) || unknownType;
return getTypeFromInference(inference) || (inference.indexes ? getIntersectionType(inference.indexes) : unknownType);
}

function inferReverseMappedType(source: Type, target: MappedType, constraint: IndexType): Type | undefined {
Expand Down Expand Up @@ -26347,6 +26349,27 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
inferFromTypes((source as IndexedAccessType).objectType, (target as IndexedAccessType).objectType);
inferFromTypes((source as IndexedAccessType).indexType, (target as IndexedAccessType).indexType);
}
else if (!(priority & InferencePriority.NakedTypeVariable) && target.flags & TypeFlags.IndexedAccess) {
if (isFromInferenceBlockedSource(source)) {
return;
}
const inference = getInferenceInfoForType(getActualTypeVariable((target as IndexedAccessType).objectType));
if (inference) {
if (getObjectFlags(source) & ObjectFlags.NonInferrableType || source === nonInferrableAnyType) {
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 inferenceTypeSymbol = getGlobalSymbol("PartialInference" as __String, SymbolFlags.Type, Diagnostics.Cannot_find_global_type_0);
const inferenceType = inferenceTypeSymbol && getDeclaredTypeOfSymbol(inferenceTypeSymbol);
if (inferenceType && inferenceType !== unknownType) {
const mapper = createTypeMapper(getSymbolLinks(inferenceTypeSymbol).typeParameters!, [source, (target as IndexedAccessType).indexType]);
inference.indexes = append(inference.indexes, instantiateType(inferenceType, mapper));
}
}
}
}
else if (source.flags & TypeFlags.StringMapping && target.flags & TypeFlags.StringMapping) {
if ((source as StringMappingType).symbol === (target as StringMappingType).symbol) {
inferFromTypes((source as StringMappingType).type, (target as StringMappingType).type);
Expand Down Expand Up @@ -27019,6 +27042,39 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
inferredType = preferCovariantType ? inferredCovariantType : inferredContravariantType;
fallbackType = preferCovariantType ? inferredContravariantType : inferredCovariantType;
}
else if (inference.indexes) {
let aggregateInference = getIntersectionType(inference.indexes);
const constraint = getConstraintOfTypeParameter(inference.typeParameter);
if (constraint) {
const instantiatedConstraint = instantiateType(constraint, context.nonFixingMapper);
if (instantiatedConstraint.flags & TypeFlags.Union && !context.compareTypes(aggregateInference, getTypeWithThisArgument(instantiatedConstraint, aggregateInference))) {
const discriminantProps = findDiscriminantProperties(getPropertiesOfType(aggregateInference), instantiatedConstraint);
if (discriminantProps) {
let match: Type | undefined;
findDiscriminant:
for (const p of discriminantProps) {
const candidatePropType = getTypeOfPropertyOfType(aggregateInference, p.escapedName);
for (const type of (instantiatedConstraint as UnionType).types) {
const propType = getTypeOfPropertyOfType(type, p.escapedName);
if (propType && candidatePropType && checkTypeAssignableTo(candidatePropType, propType, /*errorNode*/ undefined)) {
if (match && match !== type) {
match = undefined;
break findDiscriminant;
}
else {
match = type;
}
}
}
}
if (match) {
aggregateInference = getSpreadType(match, aggregateInference, /*symbol*/ undefined, /*propegatedFlags*/ 0, /*readonly*/ false);
}
}
}
}
inferredType = aggregateInference;
}
else if (context.flags & InferenceFlags.NoDefault) {
// We use silentNeverType as the wildcard that signals no inferences.
inferredType = silentNeverType;
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7043,6 +7043,7 @@ export interface InferenceInfo {
typeParameter: TypeParameter; // Type parameter for which inferences are being made
candidates: Type[] | undefined; // Candidates in covariant positions (or undefined)
contraCandidates: Type[] | undefined; // Candidates in contravariant positions (or undefined)
indexes: Type[] | undefined; // Partial candidates created by indexed accesses
inferredType?: Type; // Cache for resolved inferred type
priority?: InferencePriority; // Priority of current inference set
topLevel: boolean; // True if all inferences are to top level occurrences
Expand Down
1 change: 1 addition & 0 deletions src/harness/fourslashInterfaceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,7 @@ export namespace Completion {
typeEntry("Uncapitalize"),
typeEntry("NoInfer"),
interfaceEntry("ThisType"),
typeEntry("PartialInference"),
varEntry("ArrayBuffer"),
interfaceEntry("ArrayBufferTypes"),
typeEntry("ArrayBufferLike"),
Expand Down
9 changes: 9 additions & 0 deletions src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,15 @@ type NoInfer<T> = intrinsic;
*/
interface ThisType<T> {}

/**
* Type instantiated to perform partial inferences from indexed accesses
*/
type PartialInference<T, Keys extends string> = ({
[K in Keys]: {
[K1 in K]: T;
};
})[Keys];

/**
* Stores types to be used with WeakSet, WeakMap, WeakRef, and FinalizationRegistry
*/
Expand Down
Loading