From 36ad772c55cfd73f34ccee4e41a5c5a13ff297b7 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Wed, 23 Nov 2016 20:07:53 -0800 Subject: [PATCH 1/5] Type inference for isomorphic mapped types --- src/compiler/checker.ts | 87 +++++++++++++++++++++++++++++++++++------ 1 file changed, 75 insertions(+), 12 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 6d5a8b74cb3bd..f7d01d5480d27 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8425,7 +8425,7 @@ namespace ts { // results for union and intersection types for performance reasons. function couldContainTypeParameters(type: Type): boolean { const objectFlags = getObjectFlags(type); - return !!(type.flags & TypeFlags.TypeParameter || + return !!(type.flags & (TypeFlags.TypeParameter | TypeFlags.IndexedAccess) || objectFlags & ObjectFlags.Reference && forEach((type).typeArguments, couldContainTypeParameters) || objectFlags & ObjectFlags.Anonymous && type.symbol && type.symbol.flags & (SymbolFlags.Method | SymbolFlags.TypeLiteral | SymbolFlags.Class) || objectFlags & ObjectFlags.Mapped || @@ -8443,8 +8443,57 @@ namespace ts { return type === typeParameter || type.flags & TypeFlags.UnionOrIntersection && forEach((type).types, t => isTypeParameterAtTopLevel(t, typeParameter)); } - function inferTypes(context: InferenceContext, originalSource: Type, originalTarget: Type) { - const typeParameters = context.signature.typeParameters; + // Infer a suitable input type for an isomorphic mapped type { [P in keyof T]: X }. We construct + // an object type with the same set of properties as the source type, where the type of each + // property is computed by inferring from the source property type to X for a synthetic type + // parameter T[P] (i.e. we treat the type T[P] as the type parameter we're inferring for). + function inferTypeForIsomorphicMappedType(source: Type, target: MappedType): Type { + if (!isMappableType(source)) { + return source; + } + const typeParameter = getIndexedAccessType((getConstraintTypeFromMappedType(target)).type, getTypeParameterFromMappedType(target)); + const typeParameterArray = [typeParameter]; + const typeInferences = createTypeInferencesObject(); + const typeInferencesArray = [typeInferences]; + const templateType = getTemplateTypeFromMappedType(target); + const properties = getPropertiesOfType(source); + const members = createSymbolTable(properties); + let hasInferredTypes = false; + for (const prop of properties) { + const inferredPropType = inferTargetType(getTypeOfSymbol(prop)); + if (inferredPropType) { + const inferredProp = createSymbol(SymbolFlags.Property | SymbolFlags.Transient | prop.flags & SymbolFlags.Optional, prop.name); + inferredProp.declarations = prop.declarations; + inferredProp.type = inferredPropType; + inferredProp.isReadonly = isReadonlySymbol(prop); + members[prop.name] = inferredProp; + hasInferredTypes = true; + } + } + let indexInfo = getIndexInfoOfType(source, IndexKind.String); + if (indexInfo) { + const inferredIndexType = inferTargetType(indexInfo.type); + if (inferredIndexType) { + indexInfo = createIndexInfo(inferredIndexType, indexInfo.isReadonly); + hasInferredTypes = true; + } + } + return hasInferredTypes ? createAnonymousType(undefined, members, emptyArray, emptyArray, indexInfo, undefined) : source; + + function inferTargetType(sourceType: Type): Type { + typeInferences.primary = undefined; + typeInferences.secondary = undefined; + inferTypes(typeParameterArray, typeInferencesArray, sourceType, templateType); + const inferences = typeInferences.primary || typeInferences.secondary; + return inferences && getUnionType(inferences, /*subtypeReduction*/ true); + } + } + + function inferTypesWithContext(context: InferenceContext, originalSource: Type, originalTarget: Type) { + inferTypes(context.signature.typeParameters, context.inferences, originalSource, originalTarget); + } + + function inferTypes(typeParameters: Type[], typeInferences: TypeInferences[], originalSource: Type, originalTarget: Type) { let sourceStack: Type[]; let targetStack: Type[]; let depth = 0; @@ -8512,7 +8561,7 @@ namespace ts { target = removeTypesFromUnionOrIntersection(target, matchingTypes); } } - if (target.flags & TypeFlags.TypeParameter) { + if (target.flags & (TypeFlags.TypeParameter | TypeFlags.IndexedAccess)) { // If target is a type parameter, make an inference, unless the source type contains // the anyFunctionType (the wildcard type that's used to avoid contextually typing functions). // Because the anyFunctionType is internal, it should not be exposed to the user by adding @@ -8524,7 +8573,7 @@ namespace ts { } for (let i = 0; i < typeParameters.length; i++) { if (target === typeParameters[i]) { - const inferences = context.inferences[i]; + const inferences = typeInferences[i]; if (!inferences.isFixed) { // Any inferences that are made to a type parameter in a union type are inferior // to inferences made to a flat (non-union) type. This is because if we infer to @@ -8538,7 +8587,7 @@ namespace ts { if (!contains(candidates, source)) { candidates.push(source); } - if (!isTypeParameterAtTopLevel(originalTarget, target)) { + if (target.flags & TypeFlags.TypeParameter && !isTypeParameterAtTopLevel(originalTarget, target)) { inferences.topLevel = false; } } @@ -8589,15 +8638,29 @@ namespace ts { if (getObjectFlags(target) & ObjectFlags.Mapped) { const constraintType = getConstraintTypeFromMappedType(target); if (getObjectFlags(source) & ObjectFlags.Mapped) { + // We're inferring from a mapped type to a mapped type, so simply infer from constraint type to + // constraint type and from template type to template type. inferFromTypes(getConstraintTypeFromMappedType(source), constraintType); inferFromTypes(getTemplateTypeFromMappedType(source), getTemplateTypeFromMappedType(target)); return; } if (constraintType.flags & TypeFlags.TypeParameter) { + // We're inferring from some source type S to a mapped type { [P in T]: X }, where T is a type + // parameter. Infer from 'keyof S' to T and infer from a union of each property type in S to X. inferFromTypes(getIndexType(source), constraintType); inferFromTypes(getUnionType(map(getPropertiesOfType(source), getTypeOfSymbol)), getTemplateTypeFromMappedType(target)); return; } + if (constraintType.flags & TypeFlags.Index) { + // We're inferring from some source type S to an isomorphic mapped type { [P in keyof T]: X }, + // where T is a type parameter. Use inferTypeForIsomorphicMappedType to infer a suitable source + // type and then infer from that type to T. + const index = indexOf(typeParameters, (constraintType).type); + if (index >= 0 && !typeInferences[index].isFixed) { + inferFromTypes(inferTypeForIsomorphicMappedType(source, target), typeParameters[index]); + } + return; + } } source = getApparentType(source); if (source.flags & TypeFlags.Object) { @@ -12458,7 +12521,7 @@ namespace ts { const context = createInferenceContext(signature, /*inferUnionTypes*/ true); forEachMatchingParameterType(contextualSignature, signature, (source, target) => { // Type parameters from outer context referenced by source type are fixed by instantiation of the source type - inferTypes(context, instantiateType(source, contextualMapper), target); + inferTypesWithContext(context, instantiateType(source, contextualMapper), target); }); return getSignatureInstantiation(signature, getInferredTypes(context)); } @@ -12493,7 +12556,7 @@ namespace ts { if (thisType) { const thisArgumentNode = getThisArgumentOfCall(node); const thisArgumentType = thisArgumentNode ? checkExpression(thisArgumentNode) : voidType; - inferTypes(context, thisArgumentType, thisType); + inferTypesWithContext(context, thisArgumentType, thisType); } // We perform two passes over the arguments. In the first pass we infer from all arguments, but use @@ -12515,7 +12578,7 @@ namespace ts { argType = checkExpressionWithContextualType(arg, paramType, mapper); } - inferTypes(context, argType, paramType); + inferTypesWithContext(context, argType, paramType); } } @@ -12530,7 +12593,7 @@ namespace ts { if (excludeArgument[i] === false) { const arg = args[i]; const paramType = getTypeAtPosition(signature, i); - inferTypes(context, checkExpressionWithContextualType(arg, paramType, inferenceMapper), paramType); + inferTypesWithContext(context, checkExpressionWithContextualType(arg, paramType, inferenceMapper), paramType); } } } @@ -13617,7 +13680,7 @@ namespace ts { for (let i = 0; i < len; i++) { const declaration = signature.parameters[i].valueDeclaration; if (declaration.type) { - inferTypes(mapper.context, getTypeFromTypeNode(declaration.type), getTypeAtPosition(context, i)); + inferTypesWithContext(mapper.context, getTypeFromTypeNode(declaration.type), getTypeAtPosition(context, i)); } } } @@ -13703,7 +13766,7 @@ namespace ts { // T in the second overload so that we do not infer Base as a candidate for T // (inferring Base would make type argument inference inconsistent between the two // overloads). - inferTypes(mapper.context, links.type, instantiateType(contextualType, mapper)); + inferTypesWithContext(mapper.context, links.type, instantiateType(contextualType, mapper)); } } From ecd10be1775e12f1e921654cec4a60cb067b5740 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sun, 27 Nov 2016 12:48:59 -0800 Subject: [PATCH 2/5] Reorder type inference cases --- src/compiler/checker.ts | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index fcf90a11da2ff..2feb6b3f9c3cd 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8668,11 +8668,14 @@ namespace ts { function inferFromObjectTypes(source: Type, target: Type) { if (getObjectFlags(target) & ObjectFlags.Mapped) { const constraintType = getConstraintTypeFromMappedType(target); - if (getObjectFlags(source) & ObjectFlags.Mapped) { - // We're inferring from a mapped type to a mapped type, so simply infer from constraint type to - // constraint type and from template type to template type. - inferFromTypes(getConstraintTypeFromMappedType(source), constraintType); - inferFromTypes(getTemplateTypeFromMappedType(source), getTemplateTypeFromMappedType(target)); + if (constraintType.flags & TypeFlags.Index) { + // We're inferring from some source type S to an isomorphic mapped type { [P in keyof T]: X }, + // where T is a type parameter. Use inferTypeForIsomorphicMappedType to infer a suitable source + // type and then infer from that type to T. + const index = indexOf(typeParameters, (constraintType).type); + if (index >= 0 && !typeInferences[index].isFixed) { + inferFromTypes(inferTypeForIsomorphicMappedType(source, target), typeParameters[index]); + } return; } if (constraintType.flags & TypeFlags.TypeParameter) { @@ -8682,14 +8685,11 @@ namespace ts { inferFromTypes(getUnionType(map(getPropertiesOfType(source), getTypeOfSymbol)), getTemplateTypeFromMappedType(target)); return; } - if (constraintType.flags & TypeFlags.Index) { - // We're inferring from some source type S to an isomorphic mapped type { [P in keyof T]: X }, - // where T is a type parameter. Use inferTypeForIsomorphicMappedType to infer a suitable source - // type and then infer from that type to T. - const index = indexOf(typeParameters, (constraintType).type); - if (index >= 0 && !typeInferences[index].isFixed) { - inferFromTypes(inferTypeForIsomorphicMappedType(source, target), typeParameters[index]); - } + if (getObjectFlags(source) & ObjectFlags.Mapped) { + // We're inferring from a mapped type to a mapped type, so simply infer from constraint type to + // constraint type and from template type to template type. + inferFromTypes(getConstraintTypeFromMappedType(source), constraintType); + inferFromTypes(getTemplateTypeFromMappedType(source), getTemplateTypeFromMappedType(target)); return; } } From 997184708dd8df4e410f6c8d4d56badef83b427b Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sun, 27 Nov 2016 12:49:27 -0800 Subject: [PATCH 3/5] Add tests --- .../isomorphicMappedTypeInference.js | 222 ++++++++++ .../isomorphicMappedTypeInference.symbols | 334 +++++++++++++++ .../isomorphicMappedTypeInference.types | 405 ++++++++++++++++++ .../mapped/isomorphicMappedTypeInference.ts | 107 +++++ 4 files changed, 1068 insertions(+) create mode 100644 tests/baselines/reference/isomorphicMappedTypeInference.js create mode 100644 tests/baselines/reference/isomorphicMappedTypeInference.symbols create mode 100644 tests/baselines/reference/isomorphicMappedTypeInference.types create mode 100644 tests/cases/conformance/types/mapped/isomorphicMappedTypeInference.ts diff --git a/tests/baselines/reference/isomorphicMappedTypeInference.js b/tests/baselines/reference/isomorphicMappedTypeInference.js new file mode 100644 index 0000000000000..0fc646043dcb7 --- /dev/null +++ b/tests/baselines/reference/isomorphicMappedTypeInference.js @@ -0,0 +1,222 @@ +//// [isomorphicMappedTypeInference.ts] + +type Box = { + value: T; +} + +type Boxified = { + [P in keyof T]: Box; +} + +function box(x: T): Box { + return { value: x }; +} + +function unbox(x: Box): T { + return x.value; +} + +function boxify(obj: T): Boxified { + let result = {} as Boxified; + for (let k in obj) { + result[k] = box(obj[k]); + } + return result; +} + +function unboxify(obj: Boxified): T { + let result = {} as T; + for (let k in obj) { + result[k] = unbox(obj[k]); + } + return result; +} + +function assignBoxified(obj: Boxified, values: T) { + for (let k in values) { + obj[k].value = values[k]; + } +} + +function f1() { + let v = { + a: 42, + b: "hello", + c: true + }; + let b = boxify(v); + let x: number = b.a.value; +} + +function f2() { + let b = { + a: box(42), + b: box("hello"), + c: box(true) + }; + let v = unboxify(b); + let x: number = v.a; +} + +function f3() { + let b = { + a: box(42), + b: box("hello"), + c: box(true) + }; + assignBoxified(b, { c: false }); +} + +function f4() { + let b = { + a: box(42), + b: box("hello"), + c: box(true) + }; + b = boxify(unboxify(b)); + b = unboxify(boxify(b)); +} + +function makeRecord(obj: { [P in K]: T }) { + return obj; +} + +function f5(s: string) { + let b = makeRecord({ + a: box(42), + b: box("hello"), + c: box(true) + }); + let v = unboxify(b); + let x: string | number | boolean = v.a; +} + +function makeDictionary(obj: { [x: string]: T }) { + return obj; +} + +function f6(s: string) { + let b = makeDictionary({ + a: box(42), + b: box("hello"), + c: box(true) + }); + let v = unboxify(b); + let x: string | number | boolean = v[s]; +} + +//// [isomorphicMappedTypeInference.js] +function box(x) { + return { value: x }; +} +function unbox(x) { + return x.value; +} +function boxify(obj) { + var result = {}; + for (var k in obj) { + result[k] = box(obj[k]); + } + return result; +} +function unboxify(obj) { + var result = {}; + for (var k in obj) { + result[k] = unbox(obj[k]); + } + return result; +} +function assignBoxified(obj, values) { + for (var k in values) { + obj[k].value = values[k]; + } +} +function f1() { + var v = { + a: 42, + b: "hello", + c: true + }; + var b = boxify(v); + var x = b.a.value; +} +function f2() { + var b = { + a: box(42), + b: box("hello"), + c: box(true) + }; + var v = unboxify(b); + var x = v.a; +} +function f3() { + var b = { + a: box(42), + b: box("hello"), + c: box(true) + }; + assignBoxified(b, { c: false }); +} +function f4() { + var b = { + a: box(42), + b: box("hello"), + c: box(true) + }; + b = boxify(unboxify(b)); + b = unboxify(boxify(b)); +} +function makeRecord(obj) { + return obj; +} +function f5(s) { + var b = makeRecord({ + a: box(42), + b: box("hello"), + c: box(true) + }); + var v = unboxify(b); + var x = v.a; +} +function makeDictionary(obj) { + return obj; +} +function f6(s) { + var b = makeDictionary({ + a: box(42), + b: box("hello"), + c: box(true) + }); + var v = unboxify(b); + var x = v[s]; +} + + +//// [isomorphicMappedTypeInference.d.ts] +declare type Box = { + value: T; +}; +declare type Boxified = { + [P in keyof T]: Box; +}; +declare function box(x: T): Box; +declare function unbox(x: Box): T; +declare function boxify(obj: T): Boxified; +declare function unboxify(obj: Boxified): T; +declare function assignBoxified(obj: Boxified, values: T): void; +declare function f1(): void; +declare function f2(): void; +declare function f3(): void; +declare function f4(): void; +declare function makeRecord(obj: { + [P in K]: T; +}): { + [P in K]: T; +}; +declare function f5(s: string): void; +declare function makeDictionary(obj: { + [x: string]: T; +}): { + [x: string]: T; +}; +declare function f6(s: string): void; diff --git a/tests/baselines/reference/isomorphicMappedTypeInference.symbols b/tests/baselines/reference/isomorphicMappedTypeInference.symbols new file mode 100644 index 0000000000000..62dcc6316bcf7 --- /dev/null +++ b/tests/baselines/reference/isomorphicMappedTypeInference.symbols @@ -0,0 +1,334 @@ +=== tests/cases/conformance/types/mapped/isomorphicMappedTypeInference.ts === + +type Box = { +>Box : Symbol(Box, Decl(isomorphicMappedTypeInference.ts, 0, 0)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 1, 9)) + + value: T; +>value : Symbol(value, Decl(isomorphicMappedTypeInference.ts, 1, 15)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 1, 9)) +} + +type Boxified = { +>Boxified : Symbol(Boxified, Decl(isomorphicMappedTypeInference.ts, 3, 1)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 5, 14)) + + [P in keyof T]: Box; +>P : Symbol(P, Decl(isomorphicMappedTypeInference.ts, 6, 5)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 5, 14)) +>Box : Symbol(Box, Decl(isomorphicMappedTypeInference.ts, 0, 0)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 5, 14)) +>P : Symbol(P, Decl(isomorphicMappedTypeInference.ts, 6, 5)) +} + +function box(x: T): Box { +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 9, 13)) +>x : Symbol(x, Decl(isomorphicMappedTypeInference.ts, 9, 16)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 9, 13)) +>Box : Symbol(Box, Decl(isomorphicMappedTypeInference.ts, 0, 0)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 9, 13)) + + return { value: x }; +>value : Symbol(value, Decl(isomorphicMappedTypeInference.ts, 10, 12)) +>x : Symbol(x, Decl(isomorphicMappedTypeInference.ts, 9, 16)) +} + +function unbox(x: Box): T { +>unbox : Symbol(unbox, Decl(isomorphicMappedTypeInference.ts, 11, 1)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 13, 15)) +>x : Symbol(x, Decl(isomorphicMappedTypeInference.ts, 13, 18)) +>Box : Symbol(Box, Decl(isomorphicMappedTypeInference.ts, 0, 0)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 13, 15)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 13, 15)) + + return x.value; +>x.value : Symbol(value, Decl(isomorphicMappedTypeInference.ts, 1, 15)) +>x : Symbol(x, Decl(isomorphicMappedTypeInference.ts, 13, 18)) +>value : Symbol(value, Decl(isomorphicMappedTypeInference.ts, 1, 15)) +} + +function boxify(obj: T): Boxified { +>boxify : Symbol(boxify, Decl(isomorphicMappedTypeInference.ts, 15, 1)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 17, 16)) +>obj : Symbol(obj, Decl(isomorphicMappedTypeInference.ts, 17, 19)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 17, 16)) +>Boxified : Symbol(Boxified, Decl(isomorphicMappedTypeInference.ts, 3, 1)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 17, 16)) + + let result = {} as Boxified; +>result : Symbol(result, Decl(isomorphicMappedTypeInference.ts, 18, 7)) +>Boxified : Symbol(Boxified, Decl(isomorphicMappedTypeInference.ts, 3, 1)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 17, 16)) + + for (let k in obj) { +>k : Symbol(k, Decl(isomorphicMappedTypeInference.ts, 19, 12)) +>obj : Symbol(obj, Decl(isomorphicMappedTypeInference.ts, 17, 19)) + + result[k] = box(obj[k]); +>result : Symbol(result, Decl(isomorphicMappedTypeInference.ts, 18, 7)) +>k : Symbol(k, Decl(isomorphicMappedTypeInference.ts, 19, 12)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) +>obj : Symbol(obj, Decl(isomorphicMappedTypeInference.ts, 17, 19)) +>k : Symbol(k, Decl(isomorphicMappedTypeInference.ts, 19, 12)) + } + return result; +>result : Symbol(result, Decl(isomorphicMappedTypeInference.ts, 18, 7)) +} + +function unboxify(obj: Boxified): T { +>unboxify : Symbol(unboxify, Decl(isomorphicMappedTypeInference.ts, 23, 1)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 25, 18)) +>obj : Symbol(obj, Decl(isomorphicMappedTypeInference.ts, 25, 21)) +>Boxified : Symbol(Boxified, Decl(isomorphicMappedTypeInference.ts, 3, 1)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 25, 18)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 25, 18)) + + let result = {} as T; +>result : Symbol(result, Decl(isomorphicMappedTypeInference.ts, 26, 7)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 25, 18)) + + for (let k in obj) { +>k : Symbol(k, Decl(isomorphicMappedTypeInference.ts, 27, 12)) +>obj : Symbol(obj, Decl(isomorphicMappedTypeInference.ts, 25, 21)) + + result[k] = unbox(obj[k]); +>result : Symbol(result, Decl(isomorphicMappedTypeInference.ts, 26, 7)) +>k : Symbol(k, Decl(isomorphicMappedTypeInference.ts, 27, 12)) +>unbox : Symbol(unbox, Decl(isomorphicMappedTypeInference.ts, 11, 1)) +>obj : Symbol(obj, Decl(isomorphicMappedTypeInference.ts, 25, 21)) +>k : Symbol(k, Decl(isomorphicMappedTypeInference.ts, 27, 12)) + } + return result; +>result : Symbol(result, Decl(isomorphicMappedTypeInference.ts, 26, 7)) +} + +function assignBoxified(obj: Boxified, values: T) { +>assignBoxified : Symbol(assignBoxified, Decl(isomorphicMappedTypeInference.ts, 31, 1)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 33, 24)) +>obj : Symbol(obj, Decl(isomorphicMappedTypeInference.ts, 33, 27)) +>Boxified : Symbol(Boxified, Decl(isomorphicMappedTypeInference.ts, 3, 1)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 33, 24)) +>values : Symbol(values, Decl(isomorphicMappedTypeInference.ts, 33, 44)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 33, 24)) + + for (let k in values) { +>k : Symbol(k, Decl(isomorphicMappedTypeInference.ts, 34, 12)) +>values : Symbol(values, Decl(isomorphicMappedTypeInference.ts, 33, 44)) + + obj[k].value = values[k]; +>obj[k].value : Symbol(value, Decl(isomorphicMappedTypeInference.ts, 1, 15)) +>obj : Symbol(obj, Decl(isomorphicMappedTypeInference.ts, 33, 27)) +>k : Symbol(k, Decl(isomorphicMappedTypeInference.ts, 34, 12)) +>value : Symbol(value, Decl(isomorphicMappedTypeInference.ts, 1, 15)) +>values : Symbol(values, Decl(isomorphicMappedTypeInference.ts, 33, 44)) +>k : Symbol(k, Decl(isomorphicMappedTypeInference.ts, 34, 12)) + } +} + +function f1() { +>f1 : Symbol(f1, Decl(isomorphicMappedTypeInference.ts, 37, 1)) + + let v = { +>v : Symbol(v, Decl(isomorphicMappedTypeInference.ts, 40, 7)) + + a: 42, +>a : Symbol(a, Decl(isomorphicMappedTypeInference.ts, 40, 13)) + + b: "hello", +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 41, 14)) + + c: true +>c : Symbol(c, Decl(isomorphicMappedTypeInference.ts, 42, 19)) + + }; + let b = boxify(v); +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 45, 7)) +>boxify : Symbol(boxify, Decl(isomorphicMappedTypeInference.ts, 15, 1)) +>v : Symbol(v, Decl(isomorphicMappedTypeInference.ts, 40, 7)) + + let x: number = b.a.value; +>x : Symbol(x, Decl(isomorphicMappedTypeInference.ts, 46, 7)) +>b.a.value : Symbol(value, Decl(isomorphicMappedTypeInference.ts, 1, 15)) +>b.a : Symbol(a) +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 45, 7)) +>a : Symbol(a) +>value : Symbol(value, Decl(isomorphicMappedTypeInference.ts, 1, 15)) +} + +function f2() { +>f2 : Symbol(f2, Decl(isomorphicMappedTypeInference.ts, 47, 1)) + + let b = { +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 50, 7)) + + a: box(42), +>a : Symbol(a, Decl(isomorphicMappedTypeInference.ts, 50, 13)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + b: box("hello"), +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 51, 19)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + c: box(true) +>c : Symbol(c, Decl(isomorphicMappedTypeInference.ts, 52, 24)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + }; + let v = unboxify(b); +>v : Symbol(v, Decl(isomorphicMappedTypeInference.ts, 55, 7)) +>unboxify : Symbol(unboxify, Decl(isomorphicMappedTypeInference.ts, 23, 1)) +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 50, 7)) + + let x: number = v.a; +>x : Symbol(x, Decl(isomorphicMappedTypeInference.ts, 56, 7)) +>v.a : Symbol(a, Decl(isomorphicMappedTypeInference.ts, 50, 13)) +>v : Symbol(v, Decl(isomorphicMappedTypeInference.ts, 55, 7)) +>a : Symbol(a, Decl(isomorphicMappedTypeInference.ts, 50, 13)) +} + +function f3() { +>f3 : Symbol(f3, Decl(isomorphicMappedTypeInference.ts, 57, 1)) + + let b = { +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 60, 7)) + + a: box(42), +>a : Symbol(a, Decl(isomorphicMappedTypeInference.ts, 60, 13)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + b: box("hello"), +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 61, 19)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + c: box(true) +>c : Symbol(c, Decl(isomorphicMappedTypeInference.ts, 62, 24)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + }; + assignBoxified(b, { c: false }); +>assignBoxified : Symbol(assignBoxified, Decl(isomorphicMappedTypeInference.ts, 31, 1)) +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 60, 7)) +>c : Symbol(c, Decl(isomorphicMappedTypeInference.ts, 65, 23)) +} + +function f4() { +>f4 : Symbol(f4, Decl(isomorphicMappedTypeInference.ts, 66, 1)) + + let b = { +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 69, 7)) + + a: box(42), +>a : Symbol(a, Decl(isomorphicMappedTypeInference.ts, 69, 13)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + b: box("hello"), +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 70, 19)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + c: box(true) +>c : Symbol(c, Decl(isomorphicMappedTypeInference.ts, 71, 24)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + }; + b = boxify(unboxify(b)); +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 69, 7)) +>boxify : Symbol(boxify, Decl(isomorphicMappedTypeInference.ts, 15, 1)) +>unboxify : Symbol(unboxify, Decl(isomorphicMappedTypeInference.ts, 23, 1)) +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 69, 7)) + + b = unboxify(boxify(b)); +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 69, 7)) +>unboxify : Symbol(unboxify, Decl(isomorphicMappedTypeInference.ts, 23, 1)) +>boxify : Symbol(boxify, Decl(isomorphicMappedTypeInference.ts, 15, 1)) +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 69, 7)) +} + +function makeRecord(obj: { [P in K]: T }) { +>makeRecord : Symbol(makeRecord, Decl(isomorphicMappedTypeInference.ts, 76, 1)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 78, 20)) +>K : Symbol(K, Decl(isomorphicMappedTypeInference.ts, 78, 22)) +>obj : Symbol(obj, Decl(isomorphicMappedTypeInference.ts, 78, 41)) +>P : Symbol(P, Decl(isomorphicMappedTypeInference.ts, 78, 49)) +>K : Symbol(K, Decl(isomorphicMappedTypeInference.ts, 78, 22)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 78, 20)) + + return obj; +>obj : Symbol(obj, Decl(isomorphicMappedTypeInference.ts, 78, 41)) +} + +function f5(s: string) { +>f5 : Symbol(f5, Decl(isomorphicMappedTypeInference.ts, 80, 1)) +>s : Symbol(s, Decl(isomorphicMappedTypeInference.ts, 82, 12)) + + let b = makeRecord({ +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 83, 7)) +>makeRecord : Symbol(makeRecord, Decl(isomorphicMappedTypeInference.ts, 76, 1)) + + a: box(42), +>a : Symbol(a, Decl(isomorphicMappedTypeInference.ts, 83, 24)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + b: box("hello"), +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 84, 19)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + c: box(true) +>c : Symbol(c, Decl(isomorphicMappedTypeInference.ts, 85, 24)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + }); + let v = unboxify(b); +>v : Symbol(v, Decl(isomorphicMappedTypeInference.ts, 88, 7)) +>unboxify : Symbol(unboxify, Decl(isomorphicMappedTypeInference.ts, 23, 1)) +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 83, 7)) + + let x: string | number | boolean = v.a; +>x : Symbol(x, Decl(isomorphicMappedTypeInference.ts, 89, 7)) +>v.a : Symbol(a) +>v : Symbol(v, Decl(isomorphicMappedTypeInference.ts, 88, 7)) +>a : Symbol(a) +} + +function makeDictionary(obj: { [x: string]: T }) { +>makeDictionary : Symbol(makeDictionary, Decl(isomorphicMappedTypeInference.ts, 90, 1)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 92, 24)) +>obj : Symbol(obj, Decl(isomorphicMappedTypeInference.ts, 92, 27)) +>x : Symbol(x, Decl(isomorphicMappedTypeInference.ts, 92, 35)) +>T : Symbol(T, Decl(isomorphicMappedTypeInference.ts, 92, 24)) + + return obj; +>obj : Symbol(obj, Decl(isomorphicMappedTypeInference.ts, 92, 27)) +} + +function f6(s: string) { +>f6 : Symbol(f6, Decl(isomorphicMappedTypeInference.ts, 94, 1)) +>s : Symbol(s, Decl(isomorphicMappedTypeInference.ts, 96, 12)) + + let b = makeDictionary({ +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 97, 7)) +>makeDictionary : Symbol(makeDictionary, Decl(isomorphicMappedTypeInference.ts, 90, 1)) + + a: box(42), +>a : Symbol(a, Decl(isomorphicMappedTypeInference.ts, 97, 28)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + b: box("hello"), +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 98, 19)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + c: box(true) +>c : Symbol(c, Decl(isomorphicMappedTypeInference.ts, 99, 24)) +>box : Symbol(box, Decl(isomorphicMappedTypeInference.ts, 7, 1)) + + }); + let v = unboxify(b); +>v : Symbol(v, Decl(isomorphicMappedTypeInference.ts, 102, 7)) +>unboxify : Symbol(unboxify, Decl(isomorphicMappedTypeInference.ts, 23, 1)) +>b : Symbol(b, Decl(isomorphicMappedTypeInference.ts, 97, 7)) + + let x: string | number | boolean = v[s]; +>x : Symbol(x, Decl(isomorphicMappedTypeInference.ts, 103, 7)) +>v : Symbol(v, Decl(isomorphicMappedTypeInference.ts, 102, 7)) +>s : Symbol(s, Decl(isomorphicMappedTypeInference.ts, 96, 12)) +} diff --git a/tests/baselines/reference/isomorphicMappedTypeInference.types b/tests/baselines/reference/isomorphicMappedTypeInference.types new file mode 100644 index 0000000000000..00a80639f732c --- /dev/null +++ b/tests/baselines/reference/isomorphicMappedTypeInference.types @@ -0,0 +1,405 @@ +=== tests/cases/conformance/types/mapped/isomorphicMappedTypeInference.ts === + +type Box = { +>Box : Box +>T : T + + value: T; +>value : T +>T : T +} + +type Boxified = { +>Boxified : Boxified +>T : T + + [P in keyof T]: Box; +>P : P +>T : T +>Box : Box +>T : T +>P : P +} + +function box(x: T): Box { +>box : (x: T) => Box +>T : T +>x : T +>T : T +>Box : Box +>T : T + + return { value: x }; +>{ value: x } : { value: T; } +>value : T +>x : T +} + +function unbox(x: Box): T { +>unbox : (x: Box) => T +>T : T +>x : Box +>Box : Box +>T : T +>T : T + + return x.value; +>x.value : T +>x : Box +>value : T +} + +function boxify(obj: T): Boxified { +>boxify : (obj: T) => Boxified +>T : T +>obj : T +>T : T +>Boxified : Boxified +>T : T + + let result = {} as Boxified; +>result : Boxified +>{} as Boxified : Boxified +>{} : {} +>Boxified : Boxified +>T : T + + for (let k in obj) { +>k : keyof T +>obj : T + + result[k] = box(obj[k]); +>result[k] = box(obj[k]) : Box +>result[k] : Box +>result : Boxified +>k : keyof T +>box(obj[k]) : Box +>box : (x: T) => Box +>obj[k] : T[keyof T] +>obj : T +>k : keyof T + } + return result; +>result : Boxified +} + +function unboxify(obj: Boxified): T { +>unboxify : (obj: Boxified) => T +>T : T +>obj : Boxified +>Boxified : Boxified +>T : T +>T : T + + let result = {} as T; +>result : T +>{} as T : T +>{} : {} +>T : T + + for (let k in obj) { +>k : keyof T +>obj : Boxified + + result[k] = unbox(obj[k]); +>result[k] = unbox(obj[k]) : T[keyof T] +>result[k] : T[keyof T] +>result : T +>k : keyof T +>unbox(obj[k]) : T[keyof T] +>unbox : (x: Box) => T +>obj[k] : Box +>obj : Boxified +>k : keyof T + } + return result; +>result : T +} + +function assignBoxified(obj: Boxified, values: T) { +>assignBoxified : (obj: Boxified, values: T) => void +>T : T +>obj : Boxified +>Boxified : Boxified +>T : T +>values : T +>T : T + + for (let k in values) { +>k : keyof T +>values : T + + obj[k].value = values[k]; +>obj[k].value = values[k] : T[keyof T] +>obj[k].value : T[keyof T] +>obj[k] : Box +>obj : Boxified +>k : keyof T +>value : T[keyof T] +>values[k] : T[keyof T] +>values : T +>k : keyof T + } +} + +function f1() { +>f1 : () => void + + let v = { +>v : { a: number; b: string; c: boolean; } +>{ a: 42, b: "hello", c: true } : { a: number; b: string; c: boolean; } + + a: 42, +>a : number +>42 : 42 + + b: "hello", +>b : string +>"hello" : "hello" + + c: true +>c : boolean +>true : true + + }; + let b = boxify(v); +>b : Boxified<{ a: number; b: string; c: boolean; }> +>boxify(v) : Boxified<{ a: number; b: string; c: boolean; }> +>boxify : (obj: T) => Boxified +>v : { a: number; b: string; c: boolean; } + + let x: number = b.a.value; +>x : number +>b.a.value : number +>b.a : Box +>b : Boxified<{ a: number; b: string; c: boolean; }> +>a : Box +>value : number +} + +function f2() { +>f2 : () => void + + let b = { +>b : { a: Box; b: Box; c: Box; } +>{ a: box(42), b: box("hello"), c: box(true) } : { a: Box; b: Box; c: Box; } + + a: box(42), +>a : Box +>box(42) : Box +>box : (x: T) => Box +>42 : 42 + + b: box("hello"), +>b : Box +>box("hello") : Box +>box : (x: T) => Box +>"hello" : "hello" + + c: box(true) +>c : Box +>box(true) : Box +>box : (x: T) => Box +>true : true + + }; + let v = unboxify(b); +>v : { a: number; b: string; c: boolean; } +>unboxify(b) : { a: number; b: string; c: boolean; } +>unboxify : (obj: Boxified) => T +>b : { a: Box; b: Box; c: Box; } + + let x: number = v.a; +>x : number +>v.a : number +>v : { a: number; b: string; c: boolean; } +>a : number +} + +function f3() { +>f3 : () => void + + let b = { +>b : { a: Box; b: Box; c: Box; } +>{ a: box(42), b: box("hello"), c: box(true) } : { a: Box; b: Box; c: Box; } + + a: box(42), +>a : Box +>box(42) : Box +>box : (x: T) => Box +>42 : 42 + + b: box("hello"), +>b : Box +>box("hello") : Box +>box : (x: T) => Box +>"hello" : "hello" + + c: box(true) +>c : Box +>box(true) : Box +>box : (x: T) => Box +>true : true + + }; + assignBoxified(b, { c: false }); +>assignBoxified(b, { c: false }) : void +>assignBoxified : (obj: Boxified, values: T) => void +>b : { a: Box; b: Box; c: Box; } +>{ c: false } : { c: false; } +>c : boolean +>false : false +} + +function f4() { +>f4 : () => void + + let b = { +>b : { a: Box; b: Box; c: Box; } +>{ a: box(42), b: box("hello"), c: box(true) } : { a: Box; b: Box; c: Box; } + + a: box(42), +>a : Box +>box(42) : Box +>box : (x: T) => Box +>42 : 42 + + b: box("hello"), +>b : Box +>box("hello") : Box +>box : (x: T) => Box +>"hello" : "hello" + + c: box(true) +>c : Box +>box(true) : Box +>box : (x: T) => Box +>true : true + + }; + b = boxify(unboxify(b)); +>b = boxify(unboxify(b)) : Boxified<{ a: number; b: string; c: boolean; }> +>b : { a: Box; b: Box; c: Box; } +>boxify(unboxify(b)) : Boxified<{ a: number; b: string; c: boolean; }> +>boxify : (obj: T) => Boxified +>unboxify(b) : { a: number; b: string; c: boolean; } +>unboxify : (obj: Boxified) => T +>b : { a: Box; b: Box; c: Box; } + + b = unboxify(boxify(b)); +>b = unboxify(boxify(b)) : { a: Box; b: Box; c: Box; } +>b : { a: Box; b: Box; c: Box; } +>unboxify(boxify(b)) : { a: Box; b: Box; c: Box; } +>unboxify : (obj: Boxified) => T +>boxify(b) : Boxified<{ a: Box; b: Box; c: Box; }> +>boxify : (obj: T) => Boxified +>b : { a: Box; b: Box; c: Box; } +} + +function makeRecord(obj: { [P in K]: T }) { +>makeRecord : (obj: { [P in K]: T; }) => { [P in K]: T; } +>T : T +>K : K +>obj : { [P in K]: T; } +>P : P +>K : K +>T : T + + return obj; +>obj : { [P in K]: T; } +} + +function f5(s: string) { +>f5 : (s: string) => void +>s : string + + let b = makeRecord({ +>b : { a: Box | Box | Box; b: Box | Box | Box; c: Box | Box | Box; } +>makeRecord({ a: box(42), b: box("hello"), c: box(true) }) : { a: Box | Box | Box; b: Box | Box | Box; c: Box | Box | Box; } +>makeRecord : (obj: { [P in K]: T; }) => { [P in K]: T; } +>{ a: box(42), b: box("hello"), c: box(true) } : { a: Box; b: Box; c: Box; } + + a: box(42), +>a : Box +>box(42) : Box +>box : (x: T) => Box +>42 : 42 + + b: box("hello"), +>b : Box +>box("hello") : Box +>box : (x: T) => Box +>"hello" : "hello" + + c: box(true) +>c : Box +>box(true) : Box +>box : (x: T) => Box +>true : true + + }); + let v = unboxify(b); +>v : { a: string | number | boolean; b: string | number | boolean; c: string | number | boolean; } +>unboxify(b) : { a: string | number | boolean; b: string | number | boolean; c: string | number | boolean; } +>unboxify : (obj: Boxified) => T +>b : { a: Box | Box | Box; b: Box | Box | Box; c: Box | Box | Box; } + + let x: string | number | boolean = v.a; +>x : string | number | boolean +>v.a : string | number | boolean +>v : { a: string | number | boolean; b: string | number | boolean; c: string | number | boolean; } +>a : string | number | boolean +} + +function makeDictionary(obj: { [x: string]: T }) { +>makeDictionary : (obj: { [x: string]: T; }) => { [x: string]: T; } +>T : T +>obj : { [x: string]: T; } +>x : string +>T : T + + return obj; +>obj : { [x: string]: T; } +} + +function f6(s: string) { +>f6 : (s: string) => void +>s : string + + let b = makeDictionary({ +>b : { [x: string]: Box | Box | Box; } +>makeDictionary({ a: box(42), b: box("hello"), c: box(true) }) : { [x: string]: Box | Box | Box; } +>makeDictionary : (obj: { [x: string]: T; }) => { [x: string]: T; } +>{ a: box(42), b: box("hello"), c: box(true) } : { a: Box; b: Box; c: Box; } + + a: box(42), +>a : Box +>box(42) : Box +>box : (x: T) => Box +>42 : 42 + + b: box("hello"), +>b : Box +>box("hello") : Box +>box : (x: T) => Box +>"hello" : "hello" + + c: box(true) +>c : Box +>box(true) : Box +>box : (x: T) => Box +>true : true + + }); + let v = unboxify(b); +>v : { [x: string]: string | number | boolean; } +>unboxify(b) : { [x: string]: string | number | boolean; } +>unboxify : (obj: Boxified) => T +>b : { [x: string]: Box | Box | Box; } + + let x: string | number | boolean = v[s]; +>x : string | number | boolean +>v[s] : string | number | boolean +>v : { [x: string]: string | number | boolean; } +>s : string +} diff --git a/tests/cases/conformance/types/mapped/isomorphicMappedTypeInference.ts b/tests/cases/conformance/types/mapped/isomorphicMappedTypeInference.ts new file mode 100644 index 0000000000000..19a0c4889a50f --- /dev/null +++ b/tests/cases/conformance/types/mapped/isomorphicMappedTypeInference.ts @@ -0,0 +1,107 @@ +// @noimplicitany: true +// @declaration: true + +type Box = { + value: T; +} + +type Boxified = { + [P in keyof T]: Box; +} + +function box(x: T): Box { + return { value: x }; +} + +function unbox(x: Box): T { + return x.value; +} + +function boxify(obj: T): Boxified { + let result = {} as Boxified; + for (let k in obj) { + result[k] = box(obj[k]); + } + return result; +} + +function unboxify(obj: Boxified): T { + let result = {} as T; + for (let k in obj) { + result[k] = unbox(obj[k]); + } + return result; +} + +function assignBoxified(obj: Boxified, values: T) { + for (let k in values) { + obj[k].value = values[k]; + } +} + +function f1() { + let v = { + a: 42, + b: "hello", + c: true + }; + let b = boxify(v); + let x: number = b.a.value; +} + +function f2() { + let b = { + a: box(42), + b: box("hello"), + c: box(true) + }; + let v = unboxify(b); + let x: number = v.a; +} + +function f3() { + let b = { + a: box(42), + b: box("hello"), + c: box(true) + }; + assignBoxified(b, { c: false }); +} + +function f4() { + let b = { + a: box(42), + b: box("hello"), + c: box(true) + }; + b = boxify(unboxify(b)); + b = unboxify(boxify(b)); +} + +function makeRecord(obj: { [P in K]: T }) { + return obj; +} + +function f5(s: string) { + let b = makeRecord({ + a: box(42), + b: box("hello"), + c: box(true) + }); + let v = unboxify(b); + let x: string | number | boolean = v.a; +} + +function makeDictionary(obj: { [x: string]: T }) { + return obj; +} + +function f6(s: string) { + let b = makeDictionary({ + a: box(42), + b: box("hello"), + c: box(true) + }); + let v = unboxify(b); + let x: string | number | boolean = v[s]; +} \ No newline at end of file From 8ee5f7d64f1ede8d16d01d1ca4d1004e8d68f25b Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sun, 27 Nov 2016 19:49:57 -0800 Subject: [PATCH 4/5] Remove unused case in type inference --- src/compiler/checker.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2feb6b3f9c3cd..71dc4ec826e2c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8685,13 +8685,6 @@ namespace ts { inferFromTypes(getUnionType(map(getPropertiesOfType(source), getTypeOfSymbol)), getTemplateTypeFromMappedType(target)); return; } - if (getObjectFlags(source) & ObjectFlags.Mapped) { - // We're inferring from a mapped type to a mapped type, so simply infer from constraint type to - // constraint type and from template type to template type. - inferFromTypes(getConstraintTypeFromMappedType(source), constraintType); - inferFromTypes(getTemplateTypeFromMappedType(source), getTemplateTypeFromMappedType(target)); - return; - } } inferFromProperties(source, target); inferFromSignatures(source, target, SignatureKind.Call); From 9970606f355bd1fdc2ce0a5ce03716415f8391ef Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sun, 27 Nov 2016 19:58:05 -0800 Subject: [PATCH 5/5] Remove unused code in resolveMappedTypeMembers --- src/compiler/checker.ts | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 71dc4ec826e2c..d454465a7caca 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4494,7 +4494,6 @@ namespace ts { function resolveMappedTypeMembers(type: MappedType) { const members: SymbolTable = createMap(); let stringIndexInfo: IndexInfo; - let numberIndexInfo: IndexInfo; // Resolve upfront such that recursive references see an empty object type. setStructuredTypeMembers(type, emptySymbols, emptyArray, emptyArray, undefined, undefined); // In { [P in K]: T }, we refer to P as the type parameter type, K as the constraint type, @@ -4529,16 +4528,8 @@ namespace ts { else if (t.flags & TypeFlags.String) { stringIndexInfo = createIndexInfo(propType, isReadonly); } - else if (t.flags & TypeFlags.Number) { - numberIndexInfo = createIndexInfo(propType, isReadonly); - } }); - // If we created both a string and a numeric string index signature, and if the two index - // signatures have identical types, discard the redundant numeric index signature. - if (stringIndexInfo && numberIndexInfo && isTypeIdenticalTo(stringIndexInfo.type, numberIndexInfo.type)) { - numberIndexInfo = undefined; - } - setStructuredTypeMembers(type, members, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo); + setStructuredTypeMembers(type, members, emptyArray, emptyArray, stringIndexInfo, undefined); } function getTypeParameterFromMappedType(type: MappedType) {