From 6b68da1185d80a04a910240493dd7a8b5852bc93 Mon Sep 17 00:00:00 2001 From: Arthur Ozga Date: Fri, 18 Aug 2017 11:32:53 -0700 Subject: [PATCH] Revert "Fix getConstraintOfIndexedAccess" --- src/compiler/checker.ts | 18 ++--- src/compiler/core.ts | 2 +- ...ionOperatorWithConstrainedTypeParameter.js | 27 -------- ...eratorWithConstrainedTypeParameter.symbols | 54 --------------- ...OperatorWithConstrainedTypeParameter.types | 64 ------------------ ...tiveConstraintOfIndexAccessType.errors.txt | 67 ------------------- ...nonPrimitiveConstraintOfIndexAccessType.js | 67 ------------------- ...ionOperatorWithConstrainedTypeParameter.ts | 11 --- ...nonPrimitiveConstraintOfIndexAccessType.ts | 32 --------- 9 files changed, 8 insertions(+), 334 deletions(-) delete mode 100644 tests/baselines/reference/additionOperatorWithConstrainedTypeParameter.js delete mode 100644 tests/baselines/reference/additionOperatorWithConstrainedTypeParameter.symbols delete mode 100644 tests/baselines/reference/additionOperatorWithConstrainedTypeParameter.types delete mode 100644 tests/baselines/reference/nonPrimitiveConstraintOfIndexAccessType.errors.txt delete mode 100644 tests/baselines/reference/nonPrimitiveConstraintOfIndexAccessType.js delete mode 100644 tests/cases/conformance/expressions/binaryOperators/additionOperator/additionOperatorWithConstrainedTypeParameter.ts delete mode 100644 tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 41921e3c7892f..6df6b1f9ce1b9 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5911,13 +5911,7 @@ namespace ts { return transformed; } const baseObjectType = getBaseConstraintOfType(type.objectType); - const keepTypeParameterForMappedType = baseObjectType && getObjectFlags(baseObjectType) & ObjectFlags.Mapped && type.indexType.flags & TypeFlags.TypeParameter; - const baseIndexType = !keepTypeParameterForMappedType && getBaseConstraintOfType(type.indexType); - if (baseObjectType && baseIndexType === stringType && !getIndexInfoOfType(baseObjectType, IndexKind.String)) { - // getIndexedAccessType returns `any` for X[string] where X doesn't have an index signature. - // instead, return undefined so that the indexed access check fails - return undefined; - } + const baseIndexType = getBaseConstraintOfType(type.indexType); return baseObjectType || baseIndexType ? getIndexedAccessType(baseObjectType || type.objectType, baseIndexType || type.indexType) : undefined; } @@ -5967,9 +5961,8 @@ namespace ts { function computeBaseConstraint(t: Type): Type { if (t.flags & TypeFlags.TypeParameter) { const constraint = getConstraintFromTypeParameter(t); - return (t as TypeParameter).isThisType || !constraint ? - constraint : - getBaseConstraint(constraint); + return (t).isThisType ? constraint : + constraint ? getBaseConstraint(constraint) : undefined; } if (t.flags & TypeFlags.UnionOrIntersection) { const types = (t).types; @@ -5997,6 +5990,9 @@ namespace ts { const baseIndexedAccess = baseObjectType && baseIndexType ? getIndexedAccessType(baseObjectType, baseIndexType) : undefined; return baseIndexedAccess && baseIndexedAccess !== unknownType ? getBaseConstraint(baseIndexedAccess) : undefined; } + if (isGenericMappedType(t)) { + return emptyObjectType; + } return t; } } @@ -9293,7 +9289,7 @@ namespace ts { } else if (target.flags & TypeFlags.IndexedAccess) { // A type S is related to a type T[K] if S is related to A[K], where K is string-like and - // A is the apparent type of T. + // A is the apparent type of S. const constraint = getConstraintOfType(target); if (constraint) { if (result = isRelatedTo(source, constraint, reportErrors)) { diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 422df2ead0504..8cab1b4115502 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -718,7 +718,7 @@ namespace ts { export function sum, K extends string>(array: T[], prop: K): number { let result = 0; for (const v of array) { - // TODO: Remove the following type assertion once the fix for #17069 is merged + // Note: we need the following type assertion because of GH #17069 result += v[prop] as number; } return result; diff --git a/tests/baselines/reference/additionOperatorWithConstrainedTypeParameter.js b/tests/baselines/reference/additionOperatorWithConstrainedTypeParameter.js deleted file mode 100644 index 1366e242182e4..0000000000000 --- a/tests/baselines/reference/additionOperatorWithConstrainedTypeParameter.js +++ /dev/null @@ -1,27 +0,0 @@ -//// [additionOperatorWithConstrainedTypeParameter.ts] -// test for #17069 -function sum, K extends string>(n: number, v: T, k: K) { - n = n + v[k]; - n += v[k]; // += should work the same way -} -function realSum, K extends string>(n: number, vs: T[], k: K) { - for (const v of vs) { - n = n + v[k]; - n += v[k]; - } -} - - -//// [additionOperatorWithConstrainedTypeParameter.js] -// test for #17069 -function sum(n, v, k) { - n = n + v[k]; - n += v[k]; // += should work the same way -} -function realSum(n, vs, k) { - for (var _i = 0, vs_1 = vs; _i < vs_1.length; _i++) { - var v = vs_1[_i]; - n = n + v[k]; - n += v[k]; - } -} diff --git a/tests/baselines/reference/additionOperatorWithConstrainedTypeParameter.symbols b/tests/baselines/reference/additionOperatorWithConstrainedTypeParameter.symbols deleted file mode 100644 index e7055c1e38f8d..0000000000000 --- a/tests/baselines/reference/additionOperatorWithConstrainedTypeParameter.symbols +++ /dev/null @@ -1,54 +0,0 @@ -=== tests/cases/conformance/expressions/binaryOperators/additionOperator/additionOperatorWithConstrainedTypeParameter.ts === -// test for #17069 -function sum, K extends string>(n: number, v: T, k: K) { ->sum : Symbol(sum, Decl(additionOperatorWithConstrainedTypeParameter.ts, 0, 0)) ->T : Symbol(T, Decl(additionOperatorWithConstrainedTypeParameter.ts, 1, 13)) ->Record : Symbol(Record, Decl(lib.d.ts, --, --)) ->K : Symbol(K, Decl(additionOperatorWithConstrainedTypeParameter.ts, 1, 41)) ->K : Symbol(K, Decl(additionOperatorWithConstrainedTypeParameter.ts, 1, 41)) ->n : Symbol(n, Decl(additionOperatorWithConstrainedTypeParameter.ts, 1, 60)) ->v : Symbol(v, Decl(additionOperatorWithConstrainedTypeParameter.ts, 1, 70)) ->T : Symbol(T, Decl(additionOperatorWithConstrainedTypeParameter.ts, 1, 13)) ->k : Symbol(k, Decl(additionOperatorWithConstrainedTypeParameter.ts, 1, 76)) ->K : Symbol(K, Decl(additionOperatorWithConstrainedTypeParameter.ts, 1, 41)) - - n = n + v[k]; ->n : Symbol(n, Decl(additionOperatorWithConstrainedTypeParameter.ts, 1, 60)) ->n : Symbol(n, Decl(additionOperatorWithConstrainedTypeParameter.ts, 1, 60)) ->v : Symbol(v, Decl(additionOperatorWithConstrainedTypeParameter.ts, 1, 70)) ->k : Symbol(k, Decl(additionOperatorWithConstrainedTypeParameter.ts, 1, 76)) - - n += v[k]; // += should work the same way ->n : Symbol(n, Decl(additionOperatorWithConstrainedTypeParameter.ts, 1, 60)) ->v : Symbol(v, Decl(additionOperatorWithConstrainedTypeParameter.ts, 1, 70)) ->k : Symbol(k, Decl(additionOperatorWithConstrainedTypeParameter.ts, 1, 76)) -} -function realSum, K extends string>(n: number, vs: T[], k: K) { ->realSum : Symbol(realSum, Decl(additionOperatorWithConstrainedTypeParameter.ts, 4, 1)) ->T : Symbol(T, Decl(additionOperatorWithConstrainedTypeParameter.ts, 5, 17)) ->Record : Symbol(Record, Decl(lib.d.ts, --, --)) ->K : Symbol(K, Decl(additionOperatorWithConstrainedTypeParameter.ts, 5, 45)) ->K : Symbol(K, Decl(additionOperatorWithConstrainedTypeParameter.ts, 5, 45)) ->n : Symbol(n, Decl(additionOperatorWithConstrainedTypeParameter.ts, 5, 64)) ->vs : Symbol(vs, Decl(additionOperatorWithConstrainedTypeParameter.ts, 5, 74)) ->T : Symbol(T, Decl(additionOperatorWithConstrainedTypeParameter.ts, 5, 17)) ->k : Symbol(k, Decl(additionOperatorWithConstrainedTypeParameter.ts, 5, 83)) ->K : Symbol(K, Decl(additionOperatorWithConstrainedTypeParameter.ts, 5, 45)) - - for (const v of vs) { ->v : Symbol(v, Decl(additionOperatorWithConstrainedTypeParameter.ts, 6, 14)) ->vs : Symbol(vs, Decl(additionOperatorWithConstrainedTypeParameter.ts, 5, 74)) - - n = n + v[k]; ->n : Symbol(n, Decl(additionOperatorWithConstrainedTypeParameter.ts, 5, 64)) ->n : Symbol(n, Decl(additionOperatorWithConstrainedTypeParameter.ts, 5, 64)) ->v : Symbol(v, Decl(additionOperatorWithConstrainedTypeParameter.ts, 6, 14)) ->k : Symbol(k, Decl(additionOperatorWithConstrainedTypeParameter.ts, 5, 83)) - - n += v[k]; ->n : Symbol(n, Decl(additionOperatorWithConstrainedTypeParameter.ts, 5, 64)) ->v : Symbol(v, Decl(additionOperatorWithConstrainedTypeParameter.ts, 6, 14)) ->k : Symbol(k, Decl(additionOperatorWithConstrainedTypeParameter.ts, 5, 83)) - } -} - diff --git a/tests/baselines/reference/additionOperatorWithConstrainedTypeParameter.types b/tests/baselines/reference/additionOperatorWithConstrainedTypeParameter.types deleted file mode 100644 index d52c77a94fd69..0000000000000 --- a/tests/baselines/reference/additionOperatorWithConstrainedTypeParameter.types +++ /dev/null @@ -1,64 +0,0 @@ -=== tests/cases/conformance/expressions/binaryOperators/additionOperator/additionOperatorWithConstrainedTypeParameter.ts === -// test for #17069 -function sum, K extends string>(n: number, v: T, k: K) { ->sum : , K extends string>(n: number, v: T, k: K) => void ->T : T ->Record : Record ->K : K ->K : K ->n : number ->v : T ->T : T ->k : K ->K : K - - n = n + v[k]; ->n = n + v[k] : number ->n : number ->n + v[k] : number ->n : number ->v[k] : T[K] ->v : T ->k : K - - n += v[k]; // += should work the same way ->n += v[k] : number ->n : number ->v[k] : T[K] ->v : T ->k : K -} -function realSum, K extends string>(n: number, vs: T[], k: K) { ->realSum : , K extends string>(n: number, vs: T[], k: K) => void ->T : T ->Record : Record ->K : K ->K : K ->n : number ->vs : T[] ->T : T ->k : K ->K : K - - for (const v of vs) { ->v : T ->vs : T[] - - n = n + v[k]; ->n = n + v[k] : number ->n : number ->n + v[k] : number ->n : number ->v[k] : T[K] ->v : T ->k : K - - n += v[k]; ->n += v[k] : number ->n : number ->v[k] : T[K] ->v : T ->k : K - } -} - diff --git a/tests/baselines/reference/nonPrimitiveConstraintOfIndexAccessType.errors.txt b/tests/baselines/reference/nonPrimitiveConstraintOfIndexAccessType.errors.txt deleted file mode 100644 index 56781157131a4..0000000000000 --- a/tests/baselines/reference/nonPrimitiveConstraintOfIndexAccessType.errors.txt +++ /dev/null @@ -1,67 +0,0 @@ -tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts(3,5): error TS2322: Type 'string' is not assignable to type 'T[P]'. -tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts(6,5): error TS2322: Type 'string' is not assignable to type 'T[P]'. -tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts(9,5): error TS2322: Type 'string' is not assignable to type 'T[P]'. -tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts(12,5): error TS2322: Type 'string' is not assignable to type 'T[P]'. -tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts(15,5): error TS2322: Type 'string' is not assignable to type 'T[P]'. -tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts(18,5): error TS2322: Type 'string' is not assignable to type 'T[P]'. -tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts(21,5): error TS2322: Type 'string' is not assignable to type 'T[P]'. -tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts(24,5): error TS2322: Type 'string' is not assignable to type 'T[P]'. -tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts(27,5): error TS2322: Type 'string' is not assignable to type 'T[P]'. -tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts(30,5): error TS2322: Type 'string' is not assignable to type 'T[P]'. - Type 'string' is not assignable to type 'number'. - - -==== tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts (10 errors) ==== - // test for #15371 - function f(s: string, tp: T[P]): void { - tp = s; - ~~ -!!! error TS2322: Type 'string' is not assignable to type 'T[P]'. - } - function g(s: string, tp: T[P]): void { - tp = s; - ~~ -!!! error TS2322: Type 'string' is not assignable to type 'T[P]'. - } - function h(s: string, tp: T[P]): void { - tp = s; - ~~ -!!! error TS2322: Type 'string' is not assignable to type 'T[P]'. - } - function i(s: string, tp: T[P]): void { - tp = s; - ~~ -!!! error TS2322: Type 'string' is not assignable to type 'T[P]'. - } - function j(s: string, tp: T[P]): void { - tp = s; - ~~ -!!! error TS2322: Type 'string' is not assignable to type 'T[P]'. - } - function k(s: string, tp: T[P]): void { - tp = s; - ~~ -!!! error TS2322: Type 'string' is not assignable to type 'T[P]'. - } - function o(s: string, tp: T[P]): void { - tp = s; - ~~ -!!! error TS2322: Type 'string' is not assignable to type 'T[P]'. - } - function l(s: string, tp: T[P]): void { - tp = s; - ~~ -!!! error TS2322: Type 'string' is not assignable to type 'T[P]'. - } - function m(s: string, tp: T[P]): void { - tp = s; - ~~ -!!! error TS2322: Type 'string' is not assignable to type 'T[P]'. - } - function n(s: string, tp: T[P]): void { - tp = s; - ~~ -!!! error TS2322: Type 'string' is not assignable to type 'T[P]'. -!!! error TS2322: Type 'string' is not assignable to type 'number'. - } - \ No newline at end of file diff --git a/tests/baselines/reference/nonPrimitiveConstraintOfIndexAccessType.js b/tests/baselines/reference/nonPrimitiveConstraintOfIndexAccessType.js deleted file mode 100644 index 7a24345a94b11..0000000000000 --- a/tests/baselines/reference/nonPrimitiveConstraintOfIndexAccessType.js +++ /dev/null @@ -1,67 +0,0 @@ -//// [nonPrimitiveConstraintOfIndexAccessType.ts] -// test for #15371 -function f(s: string, tp: T[P]): void { - tp = s; -} -function g(s: string, tp: T[P]): void { - tp = s; -} -function h(s: string, tp: T[P]): void { - tp = s; -} -function i(s: string, tp: T[P]): void { - tp = s; -} -function j(s: string, tp: T[P]): void { - tp = s; -} -function k(s: string, tp: T[P]): void { - tp = s; -} -function o(s: string, tp: T[P]): void { - tp = s; -} -function l(s: string, tp: T[P]): void { - tp = s; -} -function m(s: string, tp: T[P]): void { - tp = s; -} -function n(s: string, tp: T[P]): void { - tp = s; -} - - -//// [nonPrimitiveConstraintOfIndexAccessType.js] -"use strict"; -// test for #15371 -function f(s, tp) { - tp = s; -} -function g(s, tp) { - tp = s; -} -function h(s, tp) { - tp = s; -} -function i(s, tp) { - tp = s; -} -function j(s, tp) { - tp = s; -} -function k(s, tp) { - tp = s; -} -function o(s, tp) { - tp = s; -} -function l(s, tp) { - tp = s; -} -function m(s, tp) { - tp = s; -} -function n(s, tp) { - tp = s; -} diff --git a/tests/cases/conformance/expressions/binaryOperators/additionOperator/additionOperatorWithConstrainedTypeParameter.ts b/tests/cases/conformance/expressions/binaryOperators/additionOperator/additionOperatorWithConstrainedTypeParameter.ts deleted file mode 100644 index 2303bb33944ad..0000000000000 --- a/tests/cases/conformance/expressions/binaryOperators/additionOperator/additionOperatorWithConstrainedTypeParameter.ts +++ /dev/null @@ -1,11 +0,0 @@ -// test for #17069 -function sum, K extends string>(n: number, v: T, k: K) { - n = n + v[k]; - n += v[k]; // += should work the same way -} -function realSum, K extends string>(n: number, vs: T[], k: K) { - for (const v of vs) { - n = n + v[k]; - n += v[k]; - } -} diff --git a/tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts b/tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts deleted file mode 100644 index 1e852f12f928e..0000000000000 --- a/tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts +++ /dev/null @@ -1,32 +0,0 @@ -// @strict: true -// test for #15371 -function f(s: string, tp: T[P]): void { - tp = s; -} -function g(s: string, tp: T[P]): void { - tp = s; -} -function h(s: string, tp: T[P]): void { - tp = s; -} -function i(s: string, tp: T[P]): void { - tp = s; -} -function j(s: string, tp: T[P]): void { - tp = s; -} -function k(s: string, tp: T[P]): void { - tp = s; -} -function o(s: string, tp: T[P]): void { - tp = s; -} -function l(s: string, tp: T[P]): void { - tp = s; -} -function m(s: string, tp: T[P]): void { - tp = s; -} -function n(s: string, tp: T[P]): void { - tp = s; -}