From 1ff6007a39a76f52cb278e93ff6e0a20b803850b Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 7 Aug 2018 14:44:26 -0700 Subject: [PATCH 01/12] unknownify accesses --- src/compiler/checker.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2b93a80b3b1da..b79f4ffc5ea21 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -9124,7 +9124,7 @@ namespace ts { markPropertyAsReferenced(prop, accessExpression, /*isThisAccess*/ accessExpression.expression.kind === SyntaxKind.ThisKeyword); if (isAssignmentTarget(accessExpression) && (isReferenceToReadonlyEntity(accessExpression, prop) || isReferenceThroughNamespaceImport(accessExpression))) { error(accessExpression.argumentExpression, Diagnostics.Cannot_assign_to_0_because_it_is_a_constant_or_a_read_only_property, symbolToString(prop)); - return errorType; + return accessNode ? errorType : unknownType; } if (cacheSymbol) { getNodeLinks(accessNode!).resolvedSymbol = prop; @@ -9182,7 +9182,7 @@ namespace ts { } } } - return anyType; + return accessNode ? errorType : unknownType; } } if (isJSLiteralType(objectType)) { @@ -9200,7 +9200,7 @@ namespace ts { error(indexNode, Diagnostics.Type_0_cannot_be_used_as_an_index_type, typeToString(indexType)); } } - return errorType; + return accessNode ? errorType : unknownType; } function isGenericObjectType(type: Type): boolean { @@ -9297,6 +9297,12 @@ namespace ts { if (objectType === wildcardType || indexType === wildcardType) { return wildcardType; } + if (objectType.flags & TypeFlags.Union) { + return mapType(objectType, t => getIndexedAccessType(t, indexType)); + } + if (objectType.flags & TypeFlags.Intersection) { + return getIntersectionType(map((objectType as IntersectionType).types, t => getIndexedAccessType(t, indexType))); + } // If the index type is generic, or if the object type is generic and doesn't originate in an expression, // we are performing a higher-order index access where we cannot meaningfully access the properties of the // object type. Note that for a generic T and a non-generic K, we eagerly resolve T[K] if it originates in @@ -9322,6 +9328,9 @@ namespace ts { const propTypes: Type[] = []; for (const t of (indexType).types) { const propType = getPropertyTypeForIndexType(apparentObjectType, t, accessNode, /*cacheSymbol*/ false); + if (propType === unknownType) { + return unknownType; + } if (propType === errorType) { return errorType; } From 87613bcca47e05f52fb94a589b87ed70058fd00e Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 7 Aug 2018 15:01:15 -0700 Subject: [PATCH 02/12] Move to simplify to break less with fewer changes --- src/compiler/checker.ts | 56 ++++------------------------------------- 1 file changed, 5 insertions(+), 51 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b79f4ffc5ea21..bf2b215ef8d72 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -9211,22 +9211,6 @@ namespace ts { return maybeTypeOfKind(type, TypeFlags.InstantiableNonPrimitive | TypeFlags.Index); } - // Return true if the given type is a non-generic object type with a string index signature and no - // other members. - function isStringIndexOnlyType(type: Type): boolean { - if (type.flags & TypeFlags.Object && !isGenericMappedType(type)) { - const t = resolveStructuredTypeMembers(type); - return t.properties.length === 0 && - t.callSignatures.length === 0 && t.constructSignatures.length === 0 && - !!t.stringIndexInfo && !t.numberIndexInfo; - } - return false; - } - - function isMappedTypeToNever(type: Type): boolean { - return !!(getObjectFlags(type) & ObjectFlags.Mapped) && getTemplateTypeFromMappedType(type as MappedType) === neverType; - } - function getSimplifiedType(type: Type): Type { return type.flags & TypeFlags.IndexedAccess ? getSimplifiedIndexedAccessType(type) : type; } @@ -9241,35 +9225,11 @@ namespace ts { // We recursively simplify the object type as it may in turn be an indexed access type. For example, with // '{ [P in T]: { [Q in U]: number } }[T][U]' we want to first simplify the inner indexed access type. const objectType = getSimplifiedType(type.objectType); - if (objectType.flags & TypeFlags.Intersection && isGenericObjectType(objectType)) { - // Given an indexed access type T[K], if T is an intersection containing one or more generic types and one or - // more object types with only a string index signature, e.g. '(U & V & { [x: string]: D })[K]', return a - // transformed type of the form '(U & V)[K] | D'. This allows us to properly reason about higher order indexed - // access types with default property values as expressed by D. - if (some((objectType).types, isStringIndexOnlyType)) { - const regularTypes: Type[] = []; - const stringIndexTypes: Type[] = []; - for (const t of (objectType).types) { - if (isStringIndexOnlyType(t)) { - stringIndexTypes.push(getIndexTypeOfType(t, IndexKind.String)!); - } - else { - regularTypes.push(t); - } - } - return type.simplified = getUnionType([ - getSimplifiedType(getIndexedAccessType(getIntersectionType(regularTypes), type.indexType)), - getIntersectionType(stringIndexTypes) - ]); - } - // Given an indexed access type T[K], if T is an intersection containing one or more generic types and one or - // more mapped types with a template type `never`, '(U & V & { [P in T]: never })[K]', return a - // transformed type that removes the never-mapped type: '(U & V)[K]'. This mirrors what would happen - // eventually anyway, but it easier to reason about. - if (some((objectType).types, isMappedTypeToNever)) { - const nonNeverTypes = filter((objectType).types, t => !isMappedTypeToNever(t)); - return type.simplified = getSimplifiedType(getIndexedAccessType(getIntersectionType(nonNeverTypes), type.indexType)); - } + if (objectType.flags & TypeFlags.Union) { + return type.simplified = mapType(objectType, t => getIndexedAccessType(t, type.indexType)); + } + if (objectType.flags & TypeFlags.Intersection) { + return type.simplified = getIntersectionType(map((objectType as IntersectionType).types, t => getIndexedAccessType(t, type.indexType))); } // If the object type is a mapped type { [P in K]: E }, where K is generic, instantiate E using a mapper // that substitutes the index type for P. For example, for an index access { [P in K]: Box }[X], we @@ -9297,12 +9257,6 @@ namespace ts { if (objectType === wildcardType || indexType === wildcardType) { return wildcardType; } - if (objectType.flags & TypeFlags.Union) { - return mapType(objectType, t => getIndexedAccessType(t, indexType)); - } - if (objectType.flags & TypeFlags.Intersection) { - return getIntersectionType(map((objectType as IntersectionType).types, t => getIndexedAccessType(t, indexType))); - } // If the index type is generic, or if the object type is generic and doesn't originate in an expression, // we are performing a higher-order index access where we cannot meaningfully access the properties of the // object type. Note that for a generic T and a non-generic K, we eagerly resolve T[K] if it originates in From c881d973f8480155f8051b5810bd7ae925344c98 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 7 Aug 2018 15:01:33 -0700 Subject: [PATCH 03/12] Accept baselines for now --- ...ropertyNamesContextualType9_ES5.errors.txt | 19 +- ...ropertyNamesContextualType9_ES6.errors.txt | 19 +- ...onditionalTypesExcessProperties.errors.txt | 10 +- .../deferredLookupTypeResolution2.errors.txt | 31 - .../indexedAccessRelation.errors.txt | 37 + .../reference/indexedAccessRelation.types | 2 +- .../reference/indexingTypesWithNever.types | 4 +- .../keyofAndIndexedAccess.errors.txt | 665 ++++++++++++++++++ .../reference/keyofAndIndexedAccess.types | 2 +- .../limitDeepInstantiations.errors.txt | 5 +- .../reference/limitDeepInstantiations.types | 2 +- .../reference/mappedTypeErrors2.errors.txt | 15 +- ...tiveConstraintOfIndexAccessType.errors.txt | 29 +- 13 files changed, 744 insertions(+), 96 deletions(-) delete mode 100644 tests/baselines/reference/deferredLookupTypeResolution2.errors.txt create mode 100644 tests/baselines/reference/indexedAccessRelation.errors.txt create mode 100644 tests/baselines/reference/keyofAndIndexedAccess.errors.txt diff --git a/tests/baselines/reference/computedPropertyNamesContextualType9_ES5.errors.txt b/tests/baselines/reference/computedPropertyNamesContextualType9_ES5.errors.txt index e76b3a704dc6e..d5fb590732513 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType9_ES5.errors.txt +++ b/tests/baselines/reference/computedPropertyNamesContextualType9_ES5.errors.txt @@ -1,21 +1,20 @@ -tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES5.ts(6,5): error TS2322: Type '{ [x: number]: string | number; }' is not assignable to type 'I'. - Index signatures are incompatible. - Type 'string | number' is not assignable to type 'boolean'. - Type 'string' is not assignable to type 'boolean'. +tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES5.ts(7,5): error TS2418: Type of computed property's value is 'string', which is not assignable to type 'boolean'. +tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES5.ts(8,5): error TS2418: Type of computed property's value is 'number', which is not assignable to type 'boolean'. -==== tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES5.ts (1 errors) ==== +==== tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES5.ts (2 errors) ==== interface I { [s: string]: boolean; [s: number]: boolean; } var o: I = { - ~ -!!! error TS2322: Type '{ [x: number]: string | number; }' is not assignable to type 'I'. -!!! error TS2322: Index signatures are incompatible. -!!! error TS2322: Type 'string | number' is not assignable to type 'boolean'. -!!! error TS2322: Type 'string' is not assignable to type 'boolean'. [+"foo"]: "", + ~~~~~~~~ +!!! error TS2418: Type of computed property's value is 'string', which is not assignable to type 'boolean'. +!!! related TS6501 tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES5.ts:2:5: The expected type comes from this index signature. [+"bar"]: 0 + ~~~~~~~~ +!!! error TS2418: Type of computed property's value is 'number', which is not assignable to type 'boolean'. +!!! related TS6501 tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES5.ts:2:5: The expected type comes from this index signature. } \ No newline at end of file diff --git a/tests/baselines/reference/computedPropertyNamesContextualType9_ES6.errors.txt b/tests/baselines/reference/computedPropertyNamesContextualType9_ES6.errors.txt index 468ad400c26cc..16b4ffb2bae37 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType9_ES6.errors.txt +++ b/tests/baselines/reference/computedPropertyNamesContextualType9_ES6.errors.txt @@ -1,21 +1,20 @@ -tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES6.ts(6,5): error TS2322: Type '{ [x: number]: string | number; }' is not assignable to type 'I'. - Index signatures are incompatible. - Type 'string | number' is not assignable to type 'boolean'. - Type 'string' is not assignable to type 'boolean'. +tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES6.ts(7,5): error TS2418: Type of computed property's value is 'string', which is not assignable to type 'boolean'. +tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES6.ts(8,5): error TS2418: Type of computed property's value is 'number', which is not assignable to type 'boolean'. -==== tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES6.ts (1 errors) ==== +==== tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES6.ts (2 errors) ==== interface I { [s: string]: boolean; [s: number]: boolean; } var o: I = { - ~ -!!! error TS2322: Type '{ [x: number]: string | number; }' is not assignable to type 'I'. -!!! error TS2322: Index signatures are incompatible. -!!! error TS2322: Type 'string | number' is not assignable to type 'boolean'. -!!! error TS2322: Type 'string' is not assignable to type 'boolean'. [+"foo"]: "", + ~~~~~~~~ +!!! error TS2418: Type of computed property's value is 'string', which is not assignable to type 'boolean'. +!!! related TS6501 tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES6.ts:2:5: The expected type comes from this index signature. [+"bar"]: 0 + ~~~~~~~~ +!!! error TS2418: Type of computed property's value is 'number', which is not assignable to type 'boolean'. +!!! related TS6501 tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES6.ts:2:5: The expected type comes from this index signature. } \ No newline at end of file diff --git a/tests/baselines/reference/conditionalTypesExcessProperties.errors.txt b/tests/baselines/reference/conditionalTypesExcessProperties.errors.txt index a01cb8c8986b7..54e8c8742a759 100644 --- a/tests/baselines/reference/conditionalTypesExcessProperties.errors.txt +++ b/tests/baselines/reference/conditionalTypesExcessProperties.errors.txt @@ -1,7 +1,7 @@ tests/cases/conformance/types/conditional/conditionalTypesExcessProperties.ts(8,5): error TS2322: Type '{ test: string; arg: A; }' is not assignable to type 'Something'. Type '{ test: string; arg: A; }' is not assignable to type 'A extends object ? { arg: A; } : { arg?: undefined; }'. -tests/cases/conformance/types/conditional/conditionalTypesExcessProperties.ts(9,33): error TS2322: Type 'A' is not assignable to type 'Something["arr"]'. - Type 'object' is not assignable to type 'Something["arr"]'. +tests/cases/conformance/types/conditional/conditionalTypesExcessProperties.ts(9,33): error TS2322: Type '{ test: string; arg: A; arr: A; }' is not assignable to type 'Something'. + Object literal may only specify known properties, and 'arr' does not exist in type 'Something'. ==== tests/cases/conformance/types/conditional/conditionalTypesExcessProperties.ts (2 errors) ==== @@ -17,8 +17,8 @@ tests/cases/conformance/types/conditional/conditionalTypesExcessProperties.ts(9, !!! error TS2322: Type '{ test: string; arg: A; }' is not assignable to type 'Something'. !!! error TS2322: Type '{ test: string; arg: A; }' is not assignable to type 'A extends object ? { arg: A; } : { arg?: undefined; }'. sa = { test: 'bye', arg: a, arr: a } // excess - ~~~ -!!! error TS2322: Type 'A' is not assignable to type 'Something["arr"]'. -!!! error TS2322: Type 'object' is not assignable to type 'Something["arr"]'. + ~~~~~~ +!!! error TS2322: Type '{ test: string; arg: A; arr: A; }' is not assignable to type 'Something'. +!!! error TS2322: Object literal may only specify known properties, and 'arr' does not exist in type 'Something'. } \ No newline at end of file diff --git a/tests/baselines/reference/deferredLookupTypeResolution2.errors.txt b/tests/baselines/reference/deferredLookupTypeResolution2.errors.txt deleted file mode 100644 index 88bcd2c434048..0000000000000 --- a/tests/baselines/reference/deferredLookupTypeResolution2.errors.txt +++ /dev/null @@ -1,31 +0,0 @@ -tests/cases/compiler/deferredLookupTypeResolution2.ts(14,13): error TS2536: Type '({ [K in Extract]: "true"; } & { [key: string]: "false"; })["1"]' cannot be used to index type '{ true: "true"; }'. -tests/cases/compiler/deferredLookupTypeResolution2.ts(19,21): error TS2536: Type '({ true: "otherwise"; } & { [k: string]: "true"; })[({ [K in Extract]: "true"; } & { [key: string]: "false"; })["1"]]' cannot be used to index type '{ true: "true"; }'. - - -==== tests/cases/compiler/deferredLookupTypeResolution2.ts (2 errors) ==== - // Repro from #17456 - - type StringContains = ({ [K in S]: 'true' } & { [key: string]: 'false'})[L]; - - type ObjectHasKey = StringContains, L>; - - type A = ObjectHasKey; - - type B = ObjectHasKey<[string, number], '1'>; // "true" - type C = ObjectHasKey<[string, number], '2'>; // "false" - type D = A<[string]>; // "true" - - // Error, "false" not handled - type E = { true: 'true' }[ObjectHasKey]; - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2536: Type '({ [K in Extract]: "true"; } & { [key: string]: "false"; })["1"]' cannot be used to index type '{ true: "true"; }'. - - type Juxtapose = ({ true: 'otherwise' } & { [k: string]: 'true' })[ObjectHasKey]; - - // Error, "otherwise" is missing - type DeepError = { true: 'true' }[Juxtapose]; - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2536: Type '({ true: "otherwise"; } & { [k: string]: "true"; })[({ [K in Extract]: "true"; } & { [key: string]: "false"; })["1"]]' cannot be used to index type '{ true: "true"; }'. - - type DeepOK = { true: 'true', otherwise: 'false' }[Juxtapose]; - \ No newline at end of file diff --git a/tests/baselines/reference/indexedAccessRelation.errors.txt b/tests/baselines/reference/indexedAccessRelation.errors.txt new file mode 100644 index 0000000000000..84e65df856d28 --- /dev/null +++ b/tests/baselines/reference/indexedAccessRelation.errors.txt @@ -0,0 +1,37 @@ +tests/cases/compiler/indexedAccessRelation.ts(16,23): error TS2345: Argument of type '{ a: T; }' is not assignable to parameter of type 'Pick, "a">'. + Types of property 'a' are incompatible. + Type 'T' is not assignable to type 'S["a"] & T'. + Type 'Foo' is not assignable to type 'S["a"] & T'. + Type 'Foo' is not assignable to type 'S["a"]'. + Type 'T' is not assignable to type 'S["a"]'. + Type 'Foo' is not assignable to type 'S["a"]'. + + +==== tests/cases/compiler/indexedAccessRelation.ts (1 errors) ==== + // Repro from #14723 + + class Component { + setState(state: Pick) {} + } + + export interface State { + a?: T; + } + + class Foo {} + + class Comp extends Component> + { + foo(a: T) { + this.setState({ a: a }); + ~~~~~~~~ +!!! error TS2345: Argument of type '{ a: T; }' is not assignable to parameter of type 'Pick, "a">'. +!!! error TS2345: Types of property 'a' are incompatible. +!!! error TS2345: Type 'T' is not assignable to type 'S["a"] & T'. +!!! error TS2345: Type 'Foo' is not assignable to type 'S["a"] & T'. +!!! error TS2345: Type 'Foo' is not assignable to type 'S["a"]'. +!!! error TS2345: Type 'T' is not assignable to type 'S["a"]'. +!!! error TS2345: Type 'Foo' is not assignable to type 'S["a"]'. + } + } + \ No newline at end of file diff --git a/tests/baselines/reference/indexedAccessRelation.types b/tests/baselines/reference/indexedAccessRelation.types index 6d2ee19dda240..180cff338ccc9 100644 --- a/tests/baselines/reference/indexedAccessRelation.types +++ b/tests/baselines/reference/indexedAccessRelation.types @@ -26,7 +26,7 @@ class Comp extends Component> >a : T this.setState({ a: a }); ->this.setState({ a: a }) : void +>this.setState({ a: a }) : any >this.setState : (state: Pick, K>) => void >this : this >setState : (state: Pick, K>) => void diff --git a/tests/baselines/reference/indexingTypesWithNever.types b/tests/baselines/reference/indexingTypesWithNever.types index 52583a73ba598..234ab136f0dbb 100644 --- a/tests/baselines/reference/indexingTypesWithNever.types +++ b/tests/baselines/reference/indexingTypesWithNever.types @@ -69,8 +69,8 @@ declare function genericFn3< // Should be never const result5 = genericFn3({ g: "gtest", h: "htest" }, "g", "h"); // 'g' & 'h' will reduce to never ->result5 : any ->genericFn3({ g: "gtest", h: "htest" }, "g", "h") : any +>result5 : unknown +>genericFn3({ g: "gtest", h: "htest" }, "g", "h") : unknown >genericFn3 : (obj: T, u: U, v: V) => T[U & V] >{ g: "gtest", h: "htest" } : { g: string; h: string; } >g : string diff --git a/tests/baselines/reference/keyofAndIndexedAccess.errors.txt b/tests/baselines/reference/keyofAndIndexedAccess.errors.txt new file mode 100644 index 0000000000000..41f429364463b --- /dev/null +++ b/tests/baselines/reference/keyofAndIndexedAccess.errors.txt @@ -0,0 +1,665 @@ +tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts(618,33): error TS2345: Argument of type 'T[K]' is not assignable to parameter of type '{} | null | undefined'. + Type 'unknown' is not assignable to type '{} | null | undefined'. + Type 'unknown' is not assignable to type '{}'. + Type 'T[K]' is not assignable to type '{}'. + Type 'unknown' is not assignable to type '{}'. + + +==== tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts (1 errors) ==== + class Shape { + name: string; + width: number; + height: number; + visible: boolean; + } + + class TaggedShape extends Shape { + tag: string; + } + + class Item { + name: string; + price: number; + } + + class Options { + visible: "yes" | "no"; + } + + type Dictionary = { [x: string]: T }; + type NumericallyIndexed = { [x: number]: T }; + + const enum E { A, B, C } + + type K00 = keyof any; // string + type K01 = keyof string; // "toString" | "charAt" | ... + type K02 = keyof number; // "toString" | "toFixed" | "toExponential" | ... + type K03 = keyof boolean; // "valueOf" + type K04 = keyof void; // never + type K05 = keyof undefined; // never + type K06 = keyof null; // never + type K07 = keyof never; // never + + type K10 = keyof Shape; // "name" | "width" | "height" | "visible" + type K11 = keyof Shape[]; // "length" | "toString" | ... + type K12 = keyof Dictionary; // string + type K13 = keyof {}; // never + type K14 = keyof Object; // "constructor" | "toString" | ... + type K15 = keyof E; // "toString" | "toFixed" | "toExponential" | ... + type K16 = keyof [string, number]; // "0" | "1" | "length" | "toString" | ... + type K17 = keyof (Shape | Item); // "name" + type K18 = keyof (Shape & Item); // "name" | "width" | "height" | "visible" | "price" + type K19 = keyof NumericallyIndexed // never + + type KeyOf = keyof T; + + type K20 = KeyOf; // "name" | "width" | "height" | "visible" + type K21 = KeyOf>; // string + + type NAME = "name"; + type WIDTH_OR_HEIGHT = "width" | "height"; + + type Q10 = Shape["name"]; // string + type Q11 = Shape["width" | "height"]; // number + type Q12 = Shape["name" | "visible"]; // string | boolean + + type Q20 = Shape[NAME]; // string + type Q21 = Shape[WIDTH_OR_HEIGHT]; // number + + type Q30 = [string, number][0]; // string + type Q31 = [string, number][1]; // number + type Q32 = [string, number][2]; // string | number + type Q33 = [string, number][E.A]; // string + type Q34 = [string, number][E.B]; // number + type Q35 = [string, number][E.C]; // string | number + type Q36 = [string, number]["0"]; // string + type Q37 = [string, number]["1"]; // string + + type Q40 = (Shape | Options)["visible"]; // boolean | "yes" | "no" + type Q41 = (Shape & Options)["visible"]; // true & "yes" | true & "no" | false & "yes" | false & "no" + + type Q50 = Dictionary["howdy"]; // Shape + type Q51 = Dictionary[123]; // Shape + type Q52 = Dictionary[E.B]; // Shape + + declare let cond: boolean; + + function getProperty(obj: T, key: K) { + return obj[key]; + } + + function setProperty(obj: T, key: K, value: T[K]) { + obj[key] = value; + } + + function f10(shape: Shape) { + let name = getProperty(shape, "name"); // string + let widthOrHeight = getProperty(shape, cond ? "width" : "height"); // number + let nameOrVisible = getProperty(shape, cond ? "name" : "visible"); // string | boolean + setProperty(shape, "name", "rectangle"); + setProperty(shape, cond ? "width" : "height", 10); + setProperty(shape, cond ? "name" : "visible", true); // Technically not safe + } + + function f11(a: Shape[]) { + let len = getProperty(a, "length"); // number + setProperty(a, "length", len); + } + + function f12(t: [Shape, boolean]) { + let len = getProperty(t, "length"); + let s2 = getProperty(t, "0"); // Shape + let b2 = getProperty(t, "1"); // boolean + } + + function f13(foo: any, bar: any) { + let x = getProperty(foo, "x"); // any + let y = getProperty(foo, "100"); // any + let z = getProperty(foo, bar); // any + } + + class Component { + props: PropType; + getProperty(key: K) { + return this.props[key]; + } + setProperty(key: K, value: PropType[K]) { + this.props[key] = value; + } + } + + function f20(component: Component) { + let name = component.getProperty("name"); // string + let widthOrHeight = component.getProperty(cond ? "width" : "height"); // number + let nameOrVisible = component.getProperty(cond ? "name" : "visible"); // string | boolean + component.setProperty("name", "rectangle"); + component.setProperty(cond ? "width" : "height", 10) + component.setProperty(cond ? "name" : "visible", true); // Technically not safe + } + + function pluck(array: T[], key: K) { + return array.map(x => x[key]); + } + + function f30(shapes: Shape[]) { + let names = pluck(shapes, "name"); // string[] + let widths = pluck(shapes, "width"); // number[] + let nameOrVisibles = pluck(shapes, cond ? "name" : "visible"); // (string | boolean)[] + } + + function f31(key: K) { + const shape: Shape = { name: "foo", width: 5, height: 10, visible: true }; + return shape[key]; // Shape[K] + } + + function f32(key: K) { + const shape: Shape = { name: "foo", width: 5, height: 10, visible: true }; + return shape[key]; // Shape[K] + } + + function f33(shape: S, key: K) { + let name = getProperty(shape, "name"); + let prop = getProperty(shape, key); + return prop; + } + + function f34(ts: TaggedShape) { + let tag1 = f33(ts, "tag"); + let tag2 = getProperty(ts, "tag"); + } + + class C { + public x: string; + protected y: string; + private z: string; + } + + // Indexed access expressions have always permitted access to private and protected members. + // For consistency we also permit such access in indexed access types. + function f40(c: C) { + type X = C["x"]; + type Y = C["y"]; + type Z = C["z"]; + let x: X = c["x"]; + let y: Y = c["y"]; + let z: Z = c["z"]; + } + + function f50(k: keyof T, s: string) { + const x1 = s as keyof T; + const x2 = k as string; + } + + function f51(k: K, s: string) { + const x1 = s as keyof T; + const x2 = k as string; + } + + function f52(obj: { [x: string]: boolean }, k: Exclude, s: string, n: number) { + const x1 = obj[s]; + const x2 = obj[n]; + const x3 = obj[k]; + } + + function f53>(obj: { [x: string]: boolean }, k: K, s: string, n: number) { + const x1 = obj[s]; + const x2 = obj[n]; + const x3 = obj[k]; + } + + function f54(obj: T, key: keyof T) { + for (let s in obj[key]) { + } + const b = "foo" in obj[key]; + } + + function f55(obj: T, key: K) { + for (let s in obj[key]) { + } + const b = "foo" in obj[key]; + } + + function f60(source: T, target: T) { + for (let k in source) { + target[k] = source[k]; + } + } + + function f70(func: (k1: keyof (T | U), k2: keyof (T & U)) => void) { + func<{ a: any, b: any }, { a: any, c: any }>('a', 'a'); + func<{ a: any, b: any }, { a: any, c: any }>('a', 'b'); + func<{ a: any, b: any }, { a: any, c: any }>('a', 'c'); + } + + function f71(func: (x: T, y: U) => Partial) { + let x = func({ a: 1, b: "hello" }, { c: true }); + x.a; // number | undefined + x.b; // string | undefined + x.c; // boolean | undefined + } + + function f72(func: (x: T, y: U, k: K) => (T & U)[K]) { + let a = func({ a: 1, b: "hello" }, { c: true }, 'a'); // number + let b = func({ a: 1, b: "hello" }, { c: true }, 'b'); // string + let c = func({ a: 1, b: "hello" }, { c: true }, 'c'); // boolean + } + + function f73(func: (x: T, y: U, k: K) => (T & U)[K]) { + let a = func({ a: 1, b: "hello" }, { c: true }, 'a'); // number + let b = func({ a: 1, b: "hello" }, { c: true }, 'b'); // string + let c = func({ a: 1, b: "hello" }, { c: true }, 'c'); // boolean + } + + function f74(func: (x: T, y: U, k: K) => (T | U)[K]) { + let a = func({ a: 1, b: "hello" }, { a: 2, b: true }, 'a'); // number + let b = func({ a: 1, b: "hello" }, { a: 2, b: true }, 'b'); // string | boolean + } + + function f80(obj: T) { + let a1 = obj.a; // { x: any } + let a2 = obj['a']; // { x: any } + let a3 = obj['a'] as T['a']; // T["a"] + let x1 = obj.a.x; // any + let x2 = obj['a']['x']; // any + let x3 = obj['a']['x'] as T['a']['x']; // T["a"]["x"] + } + + function f81(obj: T) { + return obj['a']['x'] as T['a']['x']; + } + + function f82() { + let x1 = f81({ a: { x: "hello" } }); // string + let x2 = f81({ a: { x: 42 } }); // number + } + + function f83(obj: T, key: K) { + return obj[key]['x'] as T[K]['x']; + } + + function f84() { + let x1 = f83({ foo: { x: "hello" } }, "foo"); // string + let x2 = f83({ bar: { x: 42 } }, "bar"); // number + } + + class C1 { + x: number; + get(key: K) { + return this[key]; + } + set(key: K, value: this[K]) { + this[key] = value; + } + foo() { + let x1 = this.x; // number + let x2 = this["x"]; // number + let x3 = this.get("x"); // this["x"] + let x4 = getProperty(this, "x"); // this["x"] + this.x = 42; + this["x"] = 42; + this.set("x", 42); + setProperty(this, "x", 42); + } + } + + type S2 = { + a: string; + b: string; + }; + + function f90(x1: S2[keyof S2], x2: T[keyof S2], x3: S2[K], x4: T[K]) { + x1 = x2; + x1 = x3; + x1 = x4; + x2 = x1; + x2 = x3; + x2 = x4; + x3 = x1; + x3 = x2; + x3 = x4; + x4 = x1; + x4 = x2; + x4 = x3; + x1.length; + x2.length; + x3.length; + x4.length; + } + + function f91(x: T, y: T[keyof T], z: T[K]) { + let a: {}; + a = x; + a = y; + a = z; + } + + function f92(x: T, y: T[keyof T], z: T[K]) { + let a: {} | null | undefined; + a = x; + a = y; + a = z; + } + + // Repros from #12011 + + class Base { + get(prop: K) { + return this[prop]; + } + set(prop: K, value: this[K]) { + this[prop] = value; + } + } + + class Person extends Base { + parts: number; + constructor(parts: number) { + super(); + this.set("parts", parts); + } + getParts() { + return this.get("parts") + } + } + + class OtherPerson { + parts: number; + constructor(parts: number) { + setProperty(this, "parts", parts); + } + getParts() { + return getProperty(this, "parts") + } + } + + // Modified repro from #12544 + + function path(obj: T, key1: K1): T[K1]; + function path(obj: T, key1: K1, key2: K2): T[K1][K2]; + function path(obj: T, key1: K1, key2: K2, key3: K3): T[K1][K2][K3]; + function path(obj: any, ...keys: (string | number)[]): any; + function path(obj: any, ...keys: (string | number)[]): any { + let result = obj; + for (let k of keys) { + result = result[k]; + } + return result; + } + + type Thing = { + a: { x: number, y: string }, + b: boolean + }; + + + function f1(thing: Thing) { + let x1 = path(thing, 'a'); // { x: number, y: string } + let x2 = path(thing, 'a', 'y'); // string + let x3 = path(thing, 'b'); // boolean + let x4 = path(thing, ...['a', 'x']); // any + } + + // Repro from comment in #12114 + + const assignTo2 = (object: T, key1: K1, key2: K2) => + (value: T[K1][K2]) => object[key1][key2] = value; + + // Modified repro from #12573 + + declare function one(handler: (t: T) => void): T + var empty = one(() => {}) // inferred as {}, expected + + type Handlers = { [K in keyof T]: (t: T[K]) => void } + declare function on(handlerHash: Handlers): T + var hashOfEmpty1 = on({ test: () => {} }); // {} + var hashOfEmpty2 = on({ test: (x: boolean) => {} }); // { test: boolean } + + // Repro from #12624 + + interface Options1 { + data?: Data + computed?: Computed; + } + + declare class Component1 { + constructor(options: Options1); + get(key: K): (Data & Computed)[K]; + } + + let c1 = new Component1({ + data: { + hello: "" + } + }); + + c1.get("hello"); + + // Repro from #12625 + + interface Options2 { + data?: Data + computed?: Computed; + } + + declare class Component2 { + constructor(options: Options2); + get(key: K): (Data & Computed)[K]; + } + + // Repro from #12641 + + interface R { + p: number; + } + + function f(p: K) { + let a: any; + a[p].add; // any + } + + // Repro from #12651 + + type MethodDescriptor = { + name: string; + args: any[]; + returnValue: any; + } + + declare function dispatchMethod(name: M['name'], args: M['args']): M['returnValue']; + + type SomeMethodDescriptor = { + name: "someMethod"; + args: [string, number]; + returnValue: string[]; + } + + let result = dispatchMethod("someMethod", ["hello", 35]); + + // Repro from #13073 + + type KeyTypes = "a" | "b" + let MyThingy: { [key in KeyTypes]: string[] }; + + function addToMyThingy(key: S) { + MyThingy[key].push("a"); + } + + // Repro from #13102 + + type Handler = { + onChange: (name: keyof T) => void; + }; + + function onChangeGenericFunction(handler: Handler) { + handler.onChange('preset') + } + + // Repro from #13285 + + function updateIds, K extends string>( + obj: T, + idFields: K[], + idMapping: { [oldId: string]: string } + ): Record { + for (const idField of idFields) { + const newId = idMapping[obj[idField]]; + if (newId) { + obj[idField] = newId; + } + } + return obj; + } + + // Repro from #13285 + + function updateIds2( + obj: T, + key: K, + stringMap: { [oldId: string]: string } + ) { + var x = obj[key]; + stringMap[x]; // Should be OK. + } + + // Repro from #13514 + + declare function head>(list: T): T[0]; + + // Repro from #13604 + + class A { + props: T & { foo: string }; + } + + class B extends A<{ x: number}> { + f(p: this["props"]) { + p.x; + } + } + + // Repro from #13749 + + class Form { + private childFormFactories: {[K in keyof T]: (v: T[K]) => Form} + + public set(prop: K, value: T[K]) { + this.childFormFactories[prop](value) + } + } + + // Repro from #13787 + + class SampleClass

{ + public props: Readonly

; + constructor(props: P) { + this.props = Object.freeze(props); + } + } + + interface Foo { + foo: string; + } + + declare function merge(obj1: T, obj2: U): T & U; + + class AnotherSampleClass extends SampleClass { + constructor(props: T) { + const foo: Foo = { foo: "bar" }; + super(merge(props, foo)); + } + + public brokenMethod() { + this.props.foo.concat; + } + } + new AnotherSampleClass({}); + + // Positive repro from #17166 + function f3>(t: T, k: K, tk: T[K]): void { + for (let key in t) { + key = k // ok, K ==> keyof T + t[key] = tk; // ok, T[K] ==> T[keyof T] + } + } + + // # 21185 + type Predicates = { + [T in keyof TaggedRecord]: (variant: TaggedRecord[keyof TaggedRecord]) => variant is TaggedRecord[T] + } + + // Repros from #23592 + + type Example = { [K in keyof T]: T[K]["prop"] }; + type Result = Example<{ a: { prop: string }; b: { prop: number } }>; + + type Helper2 = { [K in keyof T]: Extract }; + type Example2 = { [K in keyof Helper2]: Helper2[K]["prop"] }; + type Result2 = Example2<{ 1: { prop: string }; 2: { prop: number } }>; + + // Repro from #23618 + + type DBBoolTable = { [k in K]: 0 | 1 } + enum Flag { + FLAG_1 = "flag_1", + FLAG_2 = "flag_2" + } + + type SimpleDBRecord = { staticField: number } & DBBoolTable + function getFlagsFromSimpleRecord(record: SimpleDBRecord, flags: Flag[]) { + return record[flags[0]]; + } + + type DynamicDBRecord = ({ dynamicField: number } | { dynamicField: string }) & DBBoolTable + function getFlagsFromDynamicRecord(record: DynamicDBRecord, flags: Flag[]) { + return record[flags[0]]; + } + + // Repro from #21368 + + interface I { + foo: string; + } + + declare function take(p: T): void; + + function fn(o: T, k: K) { + take<{} | null | undefined>(o[k]); + ~~~~ +!!! error TS2345: Argument of type 'T[K]' is not assignable to parameter of type '{} | null | undefined'. +!!! error TS2345: Type 'unknown' is not assignable to type '{} | null | undefined'. +!!! error TS2345: Type 'unknown' is not assignable to type '{}'. +!!! error TS2345: Type 'T[K]' is not assignable to type '{}'. +!!! error TS2345: Type 'unknown' is not assignable to type '{}'. + take(o[k]); + } + + // Repro from #23133 + + class Unbounded { + foo(x: T[keyof T]) { + let y: {} | undefined | null = x; + } + } + + // Repro from #23940 + + interface I7 { + x: any; + } + type Foo7 = T; + declare function f7(type: K): Foo7; + + // Repro from #21770 + + type Dict = { [key in T]: number }; + type DictDict = { [key in V]: Dict }; + + function ff1(dd: DictDict, k1: V, k2: T): number { + return dd[k1][k2]; + } + + function ff2(dd: DictDict, k1: V, k2: T): number { + const d: Dict = dd[k1]; + return d[k2]; + } + \ No newline at end of file diff --git a/tests/baselines/reference/keyofAndIndexedAccess.types b/tests/baselines/reference/keyofAndIndexedAccess.types index 3434d003d54d6..8cb00c8a577fb 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess.types +++ b/tests/baselines/reference/keyofAndIndexedAccess.types @@ -2100,7 +2100,7 @@ function fn(o: T, k: K) { >k : K take<{} | null | undefined>(o[k]); ->take<{} | null | undefined>(o[k]) : void +>take<{} | null | undefined>(o[k]) : any >take : (p: T) => void >null : null >o[k] : T[K] diff --git a/tests/baselines/reference/limitDeepInstantiations.errors.txt b/tests/baselines/reference/limitDeepInstantiations.errors.txt index 2b68b78743704..2cf5aade7fd61 100644 --- a/tests/baselines/reference/limitDeepInstantiations.errors.txt +++ b/tests/baselines/reference/limitDeepInstantiations.errors.txt @@ -1,7 +1,8 @@ tests/cases/compiler/limitDeepInstantiations.ts(3,35): error TS2502: '"true"' is referenced directly or indirectly in its own type annotation. +tests/cases/compiler/limitDeepInstantiations.ts(5,13): error TS2344: Type '"false"' does not satisfy the constraint '"true"'. -==== tests/cases/compiler/limitDeepInstantiations.ts (1 errors) ==== +==== tests/cases/compiler/limitDeepInstantiations.ts (2 errors) ==== // Repro from #14837 type Foo = { "true": Foo> }[T]; @@ -9,4 +10,6 @@ tests/cases/compiler/limitDeepInstantiations.ts(3,35): error TS2502: '"true"' is !!! error TS2502: '"true"' is referenced directly or indirectly in its own type annotation. let f1: Foo<"true", {}>; let f2: Foo<"false", {}>; + ~~~~~~~ +!!! error TS2344: Type '"false"' does not satisfy the constraint '"true"'. \ No newline at end of file diff --git a/tests/baselines/reference/limitDeepInstantiations.types b/tests/baselines/reference/limitDeepInstantiations.types index 04899df77dbb4..bce090db14b26 100644 --- a/tests/baselines/reference/limitDeepInstantiations.types +++ b/tests/baselines/reference/limitDeepInstantiations.types @@ -9,5 +9,5 @@ let f1: Foo<"true", {}>; >f1 : any let f2: Foo<"false", {}>; ->f2 : any +>f2 : unknown diff --git a/tests/baselines/reference/mappedTypeErrors2.errors.txt b/tests/baselines/reference/mappedTypeErrors2.errors.txt index 9178731d1894d..12209cd84ac17 100644 --- a/tests/baselines/reference/mappedTypeErrors2.errors.txt +++ b/tests/baselines/reference/mappedTypeErrors2.errors.txt @@ -1,13 +1,15 @@ tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(9,30): error TS2536: Type 'K' cannot be used to index type 'T1'. tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(13,30): error TS2536: Type 'K' cannot be used to index type 'T3'. -tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(15,38): error TS2536: Type 'S' cannot be used to index type '{ [key in AB[S]]: true; }'. tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(15,47): error TS2322: Type 'AB[S]' is not assignable to type 'string | number | symbol'. - Type 'AB[S]' is not assignable to type 'symbol'. + Type 'unknown' is not assignable to type 'string | number | symbol'. + Type 'unknown' is not assignable to type 'symbol'. + Type 'AB[S]' is not assignable to type 'symbol'. + Type 'unknown' is not assignable to type 'symbol'. tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(15,47): error TS2536: Type 'S' cannot be used to index type 'AB'. tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(17,49): error TS2536: Type 'L' cannot be used to index type '{ [key in AB[S]]: true; }'. -==== tests/cases/conformance/types/mapped/mappedTypeErrors2.ts (6 errors) ==== +==== tests/cases/conformance/types/mapped/mappedTypeErrors2.ts (5 errors) ==== // Repros from #17238 type AB = { @@ -27,11 +29,12 @@ tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(17,49): error TS2536: !!! error TS2536: Type 'K' cannot be used to index type 'T3'. type T5 = {[key in AB[S]]: true}[S]; // Error - ~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2536: Type 'S' cannot be used to index type '{ [key in AB[S]]: true; }'. ~~~~~ !!! error TS2322: Type 'AB[S]' is not assignable to type 'string | number | symbol'. -!!! error TS2322: Type 'AB[S]' is not assignable to type 'symbol'. +!!! error TS2322: Type 'unknown' is not assignable to type 'string | number | symbol'. +!!! error TS2322: Type 'unknown' is not assignable to type 'symbol'. +!!! error TS2322: Type 'AB[S]' is not assignable to type 'symbol'. +!!! error TS2322: Type 'unknown' is not assignable to type 'symbol'. ~~~~~ !!! error TS2536: Type 'S' cannot be used to index type 'AB'. diff --git a/tests/baselines/reference/nonPrimitiveConstraintOfIndexAccessType.errors.txt b/tests/baselines/reference/nonPrimitiveConstraintOfIndexAccessType.errors.txt index 56781157131a4..be402415f6849 100644 --- a/tests/baselines/reference/nonPrimitiveConstraintOfIndexAccessType.errors.txt +++ b/tests/baselines/reference/nonPrimitiveConstraintOfIndexAccessType.errors.txt @@ -1,62 +1,35 @@ -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) ==== +==== tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts (1 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; From 518046c72b78bd025705af228fe740ab25f39cef Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 7 Aug 2018 18:36:02 -0700 Subject: [PATCH 04/12] Always propegate any as an index type --- src/compiler/checker.ts | 3 + .../varianceCallbacksAndIndexedAccesses.js | 36 ++++++ ...arianceCallbacksAndIndexedAccesses.symbols | 121 ++++++++++++++++++ .../varianceCallbacksAndIndexedAccesses.types | 81 ++++++++++++ .../varianceCallbacksAndIndexedAccesses.ts | 32 +++++ 5 files changed, 273 insertions(+) create mode 100644 tests/baselines/reference/varianceCallbacksAndIndexedAccesses.js create mode 100644 tests/baselines/reference/varianceCallbacksAndIndexedAccesses.symbols create mode 100644 tests/baselines/reference/varianceCallbacksAndIndexedAccesses.types create mode 100644 tests/cases/compiler/varianceCallbacksAndIndexedAccesses.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index bf2b215ef8d72..c60e3069aa68f 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -9200,6 +9200,9 @@ namespace ts { error(indexNode, Diagnostics.Type_0_cannot_be_used_as_an_index_type, typeToString(indexType)); } } + if (isTypeAny(indexType)) { + return indexType; + } return accessNode ? errorType : unknownType; } diff --git a/tests/baselines/reference/varianceCallbacksAndIndexedAccesses.js b/tests/baselines/reference/varianceCallbacksAndIndexedAccesses.js new file mode 100644 index 0000000000000..ec188824455a1 --- /dev/null +++ b/tests/baselines/reference/varianceCallbacksAndIndexedAccesses.js @@ -0,0 +1,36 @@ +//// [varianceCallbacksAndIndexedAccesses.ts] +type Source = { + (type: K, listener: (this: Window, ev: WindowEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; + (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; +} + +interface Action1 { + (arg: T): void; +} +interface MessageEventLike { + source: WindowLike; + origin: string; + data: T; +} +interface PostMessageObject { + postMessage(message: T, host: string): void; +} +interface WindowLike extends PostMessageObject { + addEventListener(type: "message", handler: Action1>): void; + addEventListener(type: string, handler: Action1): void; + removeEventListener(type: "message", handler: Action1>): void; + removeEventListener(type: string, handler: Action1): void; +} +type Target = { + (type: "message", handler: Action1>): void; + (type: string, handler: Action1): void; +}; + +function f1(s: Source, t: Target) { + t = s; +} + +//// [varianceCallbacksAndIndexedAccesses.js] +function f1(s, t) { + t = s; +} diff --git a/tests/baselines/reference/varianceCallbacksAndIndexedAccesses.symbols b/tests/baselines/reference/varianceCallbacksAndIndexedAccesses.symbols new file mode 100644 index 0000000000000..343d146b5fdf4 --- /dev/null +++ b/tests/baselines/reference/varianceCallbacksAndIndexedAccesses.symbols @@ -0,0 +1,121 @@ +=== tests/cases/compiler/varianceCallbacksAndIndexedAccesses.ts === +type Source = { +>Source : Symbol(Source, Decl(varianceCallbacksAndIndexedAccesses.ts, 0, 0)) + + (type: K, listener: (this: Window, ev: WindowEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; +>K : Symbol(K, Decl(varianceCallbacksAndIndexedAccesses.ts, 1, 5)) +>WindowEventMap : Symbol(WindowEventMap, Decl(lib.dom.d.ts, --, --)) +>type : Symbol(type, Decl(varianceCallbacksAndIndexedAccesses.ts, 1, 37)) +>K : Symbol(K, Decl(varianceCallbacksAndIndexedAccesses.ts, 1, 5)) +>listener : Symbol(listener, Decl(varianceCallbacksAndIndexedAccesses.ts, 1, 45)) +>this : Symbol(this, Decl(varianceCallbacksAndIndexedAccesses.ts, 1, 57)) +>Window : Symbol(Window, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --)) +>ev : Symbol(ev, Decl(varianceCallbacksAndIndexedAccesses.ts, 1, 70)) +>WindowEventMap : Symbol(WindowEventMap, Decl(lib.dom.d.ts, --, --)) +>K : Symbol(K, Decl(varianceCallbacksAndIndexedAccesses.ts, 1, 5)) +>options : Symbol(options, Decl(varianceCallbacksAndIndexedAccesses.ts, 1, 101)) +>AddEventListenerOptions : Symbol(AddEventListenerOptions, Decl(lib.dom.d.ts, --, --)) + + (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; +>type : Symbol(type, Decl(varianceCallbacksAndIndexedAccesses.ts, 2, 3)) +>listener : Symbol(listener, Decl(varianceCallbacksAndIndexedAccesses.ts, 2, 16)) +>EventListenerOrEventListenerObject : Symbol(EventListenerOrEventListenerObject, Decl(lib.dom.d.ts, --, --)) +>options : Symbol(options, Decl(varianceCallbacksAndIndexedAccesses.ts, 2, 62)) +>AddEventListenerOptions : Symbol(AddEventListenerOptions, Decl(lib.dom.d.ts, --, --)) +} + +interface Action1 { +>Action1 : Symbol(Action1, Decl(varianceCallbacksAndIndexedAccesses.ts, 3, 1)) +>T : Symbol(T, Decl(varianceCallbacksAndIndexedAccesses.ts, 5, 18)) + + (arg: T): void; +>arg : Symbol(arg, Decl(varianceCallbacksAndIndexedAccesses.ts, 6, 5)) +>T : Symbol(T, Decl(varianceCallbacksAndIndexedAccesses.ts, 5, 18)) +} +interface MessageEventLike { +>MessageEventLike : Symbol(MessageEventLike, Decl(varianceCallbacksAndIndexedAccesses.ts, 7, 1)) +>T : Symbol(T, Decl(varianceCallbacksAndIndexedAccesses.ts, 8, 27)) + + source: WindowLike; +>source : Symbol(MessageEventLike.source, Decl(varianceCallbacksAndIndexedAccesses.ts, 8, 31)) +>WindowLike : Symbol(WindowLike, Decl(varianceCallbacksAndIndexedAccesses.ts, 15, 1)) +>T : Symbol(T, Decl(varianceCallbacksAndIndexedAccesses.ts, 8, 27)) + + origin: string; +>origin : Symbol(MessageEventLike.origin, Decl(varianceCallbacksAndIndexedAccesses.ts, 9, 26)) + + data: T; +>data : Symbol(MessageEventLike.data, Decl(varianceCallbacksAndIndexedAccesses.ts, 10, 19)) +>T : Symbol(T, Decl(varianceCallbacksAndIndexedAccesses.ts, 8, 27)) +} +interface PostMessageObject { +>PostMessageObject : Symbol(PostMessageObject, Decl(varianceCallbacksAndIndexedAccesses.ts, 12, 1)) +>T : Symbol(T, Decl(varianceCallbacksAndIndexedAccesses.ts, 13, 28)) + + postMessage(message: T, host: string): void; +>postMessage : Symbol(PostMessageObject.postMessage, Decl(varianceCallbacksAndIndexedAccesses.ts, 13, 32)) +>message : Symbol(message, Decl(varianceCallbacksAndIndexedAccesses.ts, 14, 16)) +>T : Symbol(T, Decl(varianceCallbacksAndIndexedAccesses.ts, 13, 28)) +>host : Symbol(host, Decl(varianceCallbacksAndIndexedAccesses.ts, 14, 27)) +} +interface WindowLike extends PostMessageObject { +>WindowLike : Symbol(WindowLike, Decl(varianceCallbacksAndIndexedAccesses.ts, 15, 1)) +>T : Symbol(T, Decl(varianceCallbacksAndIndexedAccesses.ts, 16, 21)) +>PostMessageObject : Symbol(PostMessageObject, Decl(varianceCallbacksAndIndexedAccesses.ts, 12, 1)) +>T : Symbol(T, Decl(varianceCallbacksAndIndexedAccesses.ts, 16, 21)) + + addEventListener(type: "message", handler: Action1>): void; +>addEventListener : Symbol(WindowLike.addEventListener, Decl(varianceCallbacksAndIndexedAccesses.ts, 16, 54), Decl(varianceCallbacksAndIndexedAccesses.ts, 17, 83)) +>type : Symbol(type, Decl(varianceCallbacksAndIndexedAccesses.ts, 17, 21)) +>handler : Symbol(handler, Decl(varianceCallbacksAndIndexedAccesses.ts, 17, 37)) +>Action1 : Symbol(Action1, Decl(varianceCallbacksAndIndexedAccesses.ts, 3, 1)) +>MessageEventLike : Symbol(MessageEventLike, Decl(varianceCallbacksAndIndexedAccesses.ts, 7, 1)) +>T : Symbol(T, Decl(varianceCallbacksAndIndexedAccesses.ts, 16, 21)) + + addEventListener(type: string, handler: Action1): void; +>addEventListener : Symbol(WindowLike.addEventListener, Decl(varianceCallbacksAndIndexedAccesses.ts, 16, 54), Decl(varianceCallbacksAndIndexedAccesses.ts, 17, 83)) +>type : Symbol(type, Decl(varianceCallbacksAndIndexedAccesses.ts, 18, 21)) +>handler : Symbol(handler, Decl(varianceCallbacksAndIndexedAccesses.ts, 18, 34)) +>Action1 : Symbol(Action1, Decl(varianceCallbacksAndIndexedAccesses.ts, 3, 1)) + + removeEventListener(type: "message", handler: Action1>): void; +>removeEventListener : Symbol(WindowLike.removeEventListener, Decl(varianceCallbacksAndIndexedAccesses.ts, 18, 64), Decl(varianceCallbacksAndIndexedAccesses.ts, 19, 86)) +>type : Symbol(type, Decl(varianceCallbacksAndIndexedAccesses.ts, 19, 24)) +>handler : Symbol(handler, Decl(varianceCallbacksAndIndexedAccesses.ts, 19, 40)) +>Action1 : Symbol(Action1, Decl(varianceCallbacksAndIndexedAccesses.ts, 3, 1)) +>MessageEventLike : Symbol(MessageEventLike, Decl(varianceCallbacksAndIndexedAccesses.ts, 7, 1)) +>T : Symbol(T, Decl(varianceCallbacksAndIndexedAccesses.ts, 16, 21)) + + removeEventListener(type: string, handler: Action1): void; +>removeEventListener : Symbol(WindowLike.removeEventListener, Decl(varianceCallbacksAndIndexedAccesses.ts, 18, 64), Decl(varianceCallbacksAndIndexedAccesses.ts, 19, 86)) +>type : Symbol(type, Decl(varianceCallbacksAndIndexedAccesses.ts, 20, 24)) +>handler : Symbol(handler, Decl(varianceCallbacksAndIndexedAccesses.ts, 20, 37)) +>Action1 : Symbol(Action1, Decl(varianceCallbacksAndIndexedAccesses.ts, 3, 1)) +} +type Target = { +>Target : Symbol(Target, Decl(varianceCallbacksAndIndexedAccesses.ts, 21, 1)) + + (type: "message", handler: Action1>): void; +>type : Symbol(type, Decl(varianceCallbacksAndIndexedAccesses.ts, 23, 5)) +>handler : Symbol(handler, Decl(varianceCallbacksAndIndexedAccesses.ts, 23, 21)) +>Action1 : Symbol(Action1, Decl(varianceCallbacksAndIndexedAccesses.ts, 3, 1)) +>MessageEventLike : Symbol(MessageEventLike, Decl(varianceCallbacksAndIndexedAccesses.ts, 7, 1)) + + (type: string, handler: Action1): void; +>type : Symbol(type, Decl(varianceCallbacksAndIndexedAccesses.ts, 24, 5)) +>handler : Symbol(handler, Decl(varianceCallbacksAndIndexedAccesses.ts, 24, 18)) +>Action1 : Symbol(Action1, Decl(varianceCallbacksAndIndexedAccesses.ts, 3, 1)) + +}; + +function f1(s: Source, t: Target) { +>f1 : Symbol(f1, Decl(varianceCallbacksAndIndexedAccesses.ts, 25, 2)) +>s : Symbol(s, Decl(varianceCallbacksAndIndexedAccesses.ts, 27, 12)) +>Source : Symbol(Source, Decl(varianceCallbacksAndIndexedAccesses.ts, 0, 0)) +>t : Symbol(t, Decl(varianceCallbacksAndIndexedAccesses.ts, 27, 22)) +>Target : Symbol(Target, Decl(varianceCallbacksAndIndexedAccesses.ts, 21, 1)) + + t = s; +>t : Symbol(t, Decl(varianceCallbacksAndIndexedAccesses.ts, 27, 22)) +>s : Symbol(s, Decl(varianceCallbacksAndIndexedAccesses.ts, 27, 12)) +} diff --git a/tests/baselines/reference/varianceCallbacksAndIndexedAccesses.types b/tests/baselines/reference/varianceCallbacksAndIndexedAccesses.types new file mode 100644 index 0000000000000..6b4d90767b69d --- /dev/null +++ b/tests/baselines/reference/varianceCallbacksAndIndexedAccesses.types @@ -0,0 +1,81 @@ +=== tests/cases/compiler/varianceCallbacksAndIndexedAccesses.ts === +type Source = { +>Source : Source + + (type: K, listener: (this: Window, ev: WindowEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; +>type : K +>listener : (this: Window, ev: WindowEventMap[K]) => any +>this : Window +>ev : WindowEventMap[K] +>options : boolean | AddEventListenerOptions + + (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; +>type : string +>listener : EventListenerOrEventListenerObject +>options : boolean | AddEventListenerOptions +} + +interface Action1 { + (arg: T): void; +>arg : T +} +interface MessageEventLike { + source: WindowLike; +>source : WindowLike + + origin: string; +>origin : string + + data: T; +>data : T +} +interface PostMessageObject { + postMessage(message: T, host: string): void; +>postMessage : (message: T, host: string) => void +>message : T +>host : string +} +interface WindowLike extends PostMessageObject { + addEventListener(type: "message", handler: Action1>): void; +>addEventListener : { (type: "message", handler: Action1>): void; (type: string, handler: Action1): void; } +>type : "message" +>handler : Action1> + + addEventListener(type: string, handler: Action1): void; +>addEventListener : { (type: "message", handler: Action1>): void; (type: string, handler: Action1): void; } +>type : string +>handler : Action1 + + removeEventListener(type: "message", handler: Action1>): void; +>removeEventListener : { (type: "message", handler: Action1>): void; (type: string, handler: Action1): void; } +>type : "message" +>handler : Action1> + + removeEventListener(type: string, handler: Action1): void; +>removeEventListener : { (type: "message", handler: Action1>): void; (type: string, handler: Action1): void; } +>type : string +>handler : Action1 +} +type Target = { +>Target : Target + + (type: "message", handler: Action1>): void; +>type : "message" +>handler : Action1> + + (type: string, handler: Action1): void; +>type : string +>handler : Action1 + +}; + +function f1(s: Source, t: Target) { +>f1 : (s: Source, t: Target) => void +>s : Source +>t : Target + + t = s; +>t = s : Source +>t : Target +>s : Source +} diff --git a/tests/cases/compiler/varianceCallbacksAndIndexedAccesses.ts b/tests/cases/compiler/varianceCallbacksAndIndexedAccesses.ts new file mode 100644 index 0000000000000..471829ffd8174 --- /dev/null +++ b/tests/cases/compiler/varianceCallbacksAndIndexedAccesses.ts @@ -0,0 +1,32 @@ + + +type Source = { + (type: K, listener: (this: Window, ev: WindowEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; + (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; +} + +interface Action1 { + (arg: T): void; +} +interface MessageEventLike { + source: WindowLike; + origin: string; + data: T; +} +interface PostMessageObject { + postMessage(message: T, host: string): void; +} +interface WindowLike extends PostMessageObject { + addEventListener(type: "message", handler: Action1>): void; + addEventListener(type: string, handler: Action1): void; + removeEventListener(type: "message", handler: Action1>): void; + removeEventListener(type: string, handler: Action1): void; +} +type Target = { + (type: "message", handler: Action1>): void; + (type: string, handler: Action1): void; +}; + +function f1(s: Source, t: Target) { + t = s; +} \ No newline at end of file From 17d37ec4a862f2cc28b1da3dc2bd3a278d86fa08 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Wed, 8 Aug 2018 11:26:30 -0700 Subject: [PATCH 05/12] Fix flow control in the presence of simplifiable types --- src/compiler/checker.ts | 18 ++--- ...sIndexOnExistingReadonlyFieldIsNotNever.js | 60 +++++++++++++++ ...xOnExistingReadonlyFieldIsNotNever.symbols | 76 +++++++++++++++++++ ...dexOnExistingReadonlyFieldIsNotNever.types | 57 ++++++++++++++ ...sIndexOnExistingReadonlyFieldIsNotNever.ts | 22 ++++++ 5 files changed, 224 insertions(+), 9 deletions(-) create mode 100644 tests/baselines/reference/thisIndexOnExistingReadonlyFieldIsNotNever.js create mode 100644 tests/baselines/reference/thisIndexOnExistingReadonlyFieldIsNotNever.symbols create mode 100644 tests/baselines/reference/thisIndexOnExistingReadonlyFieldIsNotNever.types create mode 100644 tests/cases/compiler/thisIndexOnExistingReadonlyFieldIsNotNever.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c60e3069aa68f..1b66cb8b820dc 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -14028,10 +14028,10 @@ namespace ts { getInitialTypeOfBindingElement(node); } - function getInitialOrAssignedType(node: VariableDeclaration | BindingElement | Expression) { - return node.kind === SyntaxKind.VariableDeclaration || node.kind === SyntaxKind.BindingElement ? + function getInitialOrAssignedType(node: VariableDeclaration | BindingElement | Expression, reference: Node) { + return getConstraintForLocation(node.kind === SyntaxKind.VariableDeclaration || node.kind === SyntaxKind.BindingElement ? getInitialType(node) : - getAssignedType(node); + getAssignedType(node), reference); } function isEmptyArrayAssignment(node: VariableDeclaration | BindingElement | Expression) { @@ -14414,11 +14414,11 @@ namespace ts { if (isEmptyArrayAssignment(node)) { return getEvolvingArrayType(neverType); } - const assignedType = getBaseTypeOfLiteralType(getInitialOrAssignedType(node)); + const assignedType = getBaseTypeOfLiteralType(getInitialOrAssignedType(node, reference)); return isTypeAssignableTo(assignedType, declaredType) ? assignedType : anyArrayType; } if (declaredType.flags & TypeFlags.Union) { - return getAssignmentReducedType(declaredType, getInitialOrAssignedType(node)); + return getAssignmentReducedType(declaredType, getInitialOrAssignedType(node, reference)); } return declaredType; } @@ -15030,14 +15030,14 @@ namespace ts { return type.flags & TypeFlags.InstantiableNonPrimitive && maybeTypeOfKind(getBaseConstraintOfType(type) || emptyObjectType, TypeFlags.Nullable); } - function getConstraintForLocation(type: Type, node: Node): Type; - function getConstraintForLocation(type: Type | undefined, node: Node): Type | undefined; - function getConstraintForLocation(type: Type, node: Node): Type | undefined { + function getConstraintForLocation(type: Type, node?: Node): Type; + function getConstraintForLocation(type: Type | undefined, node?: Node): Type | undefined; + function getConstraintForLocation(type: Type, node?: Node): Type | undefined { // When a node is the left hand expression of a property access, element access, or call expression, // and the type of the node includes type variables with constraints that are nullable, we fetch the // apparent type of the node *before* performing control flow analysis such that narrowings apply to // the constraint type. - if (type && isConstraintPosition(node) && forEachType(type, typeHasNullableConstraint)) { + if (node && type && isConstraintPosition(node) && forEachType(type, typeHasNullableConstraint)) { return mapType(getWidenedType(type), getBaseConstraintOrType); } return type; diff --git a/tests/baselines/reference/thisIndexOnExistingReadonlyFieldIsNotNever.js b/tests/baselines/reference/thisIndexOnExistingReadonlyFieldIsNotNever.js new file mode 100644 index 0000000000000..56273a2b00691 --- /dev/null +++ b/tests/baselines/reference/thisIndexOnExistingReadonlyFieldIsNotNever.js @@ -0,0 +1,60 @@ +//// [thisIndexOnExistingReadonlyFieldIsNotNever.ts] +declare class Component { + readonly props: Readonly<{ children?: unknown }> & Readonly

; + state: Readonly; +} +interface CoachMarkAnchorProps { + anchorRef?: (anchor: C) => void; +} +type AnchorType

= Component

; + +class CoachMarkAnchorDecorator { + decorateComponent

(anchor: P) { + return class CoachMarkAnchor extends Component> & P, {}> { + private _onAnchorRef = (anchor: AnchorType

) => { + const anchorRef = this.props.anchorRef; + if (anchorRef) { + anchorRef(anchor); + } + } + }; + } +} + + +//// [thisIndexOnExistingReadonlyFieldIsNotNever.js] +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; + return extendStatics(d, b); + } + return function (d, b) { + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var CoachMarkAnchorDecorator = /** @class */ (function () { + function CoachMarkAnchorDecorator() { + } + CoachMarkAnchorDecorator.prototype.decorateComponent = function (anchor) { + return /** @class */ (function (_super) { + __extends(CoachMarkAnchor, _super); + function CoachMarkAnchor() { + var _this = _super !== null && _super.apply(this, arguments) || this; + _this._onAnchorRef = function (anchor) { + var anchorRef = _this.props.anchorRef; + if (anchorRef) { + anchorRef(anchor); + } + }; + return _this; + } + return CoachMarkAnchor; + }(Component)); + }; + return CoachMarkAnchorDecorator; +}()); diff --git a/tests/baselines/reference/thisIndexOnExistingReadonlyFieldIsNotNever.symbols b/tests/baselines/reference/thisIndexOnExistingReadonlyFieldIsNotNever.symbols new file mode 100644 index 0000000000000..6cedbe269bfd8 --- /dev/null +++ b/tests/baselines/reference/thisIndexOnExistingReadonlyFieldIsNotNever.symbols @@ -0,0 +1,76 @@ +=== tests/cases/compiler/thisIndexOnExistingReadonlyFieldIsNotNever.ts === +declare class Component { +>Component : Symbol(Component, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 0, 0)) +>P : Symbol(P, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 0, 24)) +>S : Symbol(S, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 0, 26)) + + readonly props: Readonly<{ children?: unknown }> & Readonly

; +>props : Symbol(Component.props, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 0, 36)) +>Readonly : Symbol(Readonly, Decl(lib.es5.d.ts, --, --)) +>children : Symbol(children, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 1, 30)) +>Readonly : Symbol(Readonly, Decl(lib.es5.d.ts, --, --)) +>P : Symbol(P, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 0, 24)) + + state: Readonly; +>state : Symbol(Component.state, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 1, 67)) +>Readonly : Symbol(Readonly, Decl(lib.es5.d.ts, --, --)) +>S : Symbol(S, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 0, 26)) +} +interface CoachMarkAnchorProps { +>CoachMarkAnchorProps : Symbol(CoachMarkAnchorProps, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 3, 1)) +>C : Symbol(C, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 4, 31)) + + anchorRef?: (anchor: C) => void; +>anchorRef : Symbol(CoachMarkAnchorProps.anchorRef, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 4, 35)) +>anchor : Symbol(anchor, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 5, 17)) +>C : Symbol(C, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 4, 31)) +} +type AnchorType

= Component

; +>AnchorType : Symbol(AnchorType, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 6, 1)) +>P : Symbol(P, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 7, 16)) +>Component : Symbol(Component, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 0, 0)) +>P : Symbol(P, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 7, 16)) + +class CoachMarkAnchorDecorator { +>CoachMarkAnchorDecorator : Symbol(CoachMarkAnchorDecorator, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 7, 34)) + + decorateComponent

(anchor: P) { +>decorateComponent : Symbol(CoachMarkAnchorDecorator.decorateComponent, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 9, 32)) +>P : Symbol(P, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 10, 22)) +>anchor : Symbol(anchor, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 10, 25)) +>P : Symbol(P, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 10, 22)) + + return class CoachMarkAnchor extends Component> & P, {}> { +>CoachMarkAnchor : Symbol(CoachMarkAnchor, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 11, 14)) +>Component : Symbol(Component, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 0, 0)) +>CoachMarkAnchorProps : Symbol(CoachMarkAnchorProps, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 3, 1)) +>AnchorType : Symbol(AnchorType, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 6, 1)) +>P : Symbol(P, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 10, 22)) +>P : Symbol(P, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 10, 22)) + + private _onAnchorRef = (anchor: AnchorType

) => { +>_onAnchorRef : Symbol(CoachMarkAnchor._onAnchorRef, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 11, 101)) +>anchor : Symbol(anchor, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 12, 36)) +>AnchorType : Symbol(AnchorType, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 6, 1)) +>P : Symbol(P, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 10, 22)) + + const anchorRef = this.props.anchorRef; +>anchorRef : Symbol(anchorRef, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 13, 21)) +>this.props.anchorRef : Symbol(anchorRef, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 4, 35)) +>this.props : Symbol(Component.props, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 0, 36)) +>this : Symbol(CoachMarkAnchor, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 11, 14)) +>props : Symbol(Component.props, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 0, 36)) +>anchorRef : Symbol(anchorRef, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 4, 35)) + + if (anchorRef) { +>anchorRef : Symbol(anchorRef, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 13, 21)) + + anchorRef(anchor); +>anchorRef : Symbol(anchorRef, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 13, 21)) +>anchor : Symbol(anchor, Decl(thisIndexOnExistingReadonlyFieldIsNotNever.ts, 12, 36)) + } + } + }; + } +} + diff --git a/tests/baselines/reference/thisIndexOnExistingReadonlyFieldIsNotNever.types b/tests/baselines/reference/thisIndexOnExistingReadonlyFieldIsNotNever.types new file mode 100644 index 0000000000000..30dfd635724d1 --- /dev/null +++ b/tests/baselines/reference/thisIndexOnExistingReadonlyFieldIsNotNever.types @@ -0,0 +1,57 @@ +=== tests/cases/compiler/thisIndexOnExistingReadonlyFieldIsNotNever.ts === +declare class Component { +>Component : Component + + readonly props: Readonly<{ children?: unknown }> & Readonly

; +>props : Readonly<{ children?: unknown; }> & Readonly

+>children : unknown + + state: Readonly; +>state : Readonly +} +interface CoachMarkAnchorProps { + anchorRef?: (anchor: C) => void; +>anchorRef : ((anchor: C) => void) | undefined +>anchor : C +} +type AnchorType

= Component

; +>AnchorType : Component + +class CoachMarkAnchorDecorator { +>CoachMarkAnchorDecorator : CoachMarkAnchorDecorator + + decorateComponent

(anchor: P) { +>decorateComponent :

(anchor: P) => typeof CoachMarkAnchor +>anchor : P + + return class CoachMarkAnchor extends Component> & P, {}> { +>class CoachMarkAnchor extends Component> & P, {}> { private _onAnchorRef = (anchor: AnchorType

) => { const anchorRef = this.props.anchorRef; if (anchorRef) { anchorRef(anchor); } } } : typeof CoachMarkAnchor +>CoachMarkAnchor : typeof CoachMarkAnchor +>Component : Component> & P, {}> + + private _onAnchorRef = (anchor: AnchorType

) => { +>_onAnchorRef : (anchor: Component) => void +>(anchor: AnchorType

) => { const anchorRef = this.props.anchorRef; if (anchorRef) { anchorRef(anchor); } } : (anchor: Component) => void +>anchor : Component + + const anchorRef = this.props.anchorRef; +>anchorRef : (CoachMarkAnchorProps> & P)["anchorRef"] | undefined +>this.props.anchorRef : (CoachMarkAnchorProps> & P)["anchorRef"] | undefined +>this.props : Readonly<{ children?: unknown; }> & Readonly> & P> +>this : this +>props : Readonly<{ children?: unknown; }> & Readonly> & P> +>anchorRef : (CoachMarkAnchorProps> & P)["anchorRef"] | undefined + + if (anchorRef) { +>anchorRef : (CoachMarkAnchorProps> & P)["anchorRef"] | undefined + + anchorRef(anchor); +>anchorRef(anchor) : void +>anchorRef : (anchor: Component) => void +>anchor : Component + } + } + }; + } +} + diff --git a/tests/cases/compiler/thisIndexOnExistingReadonlyFieldIsNotNever.ts b/tests/cases/compiler/thisIndexOnExistingReadonlyFieldIsNotNever.ts new file mode 100644 index 0000000000000..a271cbdbc9c3e --- /dev/null +++ b/tests/cases/compiler/thisIndexOnExistingReadonlyFieldIsNotNever.ts @@ -0,0 +1,22 @@ +// @strict: true +declare class Component { + readonly props: Readonly<{ children?: unknown }> & Readonly

; + state: Readonly; +} +interface CoachMarkAnchorProps { + anchorRef?: (anchor: C) => void; +} +type AnchorType

= Component

; + +class CoachMarkAnchorDecorator { + decorateComponent

(anchor: P) { + return class CoachMarkAnchor extends Component> & P, {}> { + private _onAnchorRef = (anchor: AnchorType

) => { + const anchorRef = this.props.anchorRef; + if (anchorRef) { + anchorRef(anchor); + } + } + }; + } +} From 89b9e6ed16ba9b2e4490307072fa5222afdf6b18 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Wed, 8 Aug 2018 16:44:00 -0700 Subject: [PATCH 06/12] Add spy repro from #25181 --- .../spyComparisonChecking.errors.txt | 29 +++++++ .../reference/spyComparisonChecking.js | 34 +++++++++ .../reference/spyComparisonChecking.symbols | 75 +++++++++++++++++++ .../reference/spyComparisonChecking.types | 62 +++++++++++++++ tests/cases/compiler/spyComparisonChecking.ts | 23 ++++++ 5 files changed, 223 insertions(+) create mode 100644 tests/baselines/reference/spyComparisonChecking.errors.txt create mode 100644 tests/baselines/reference/spyComparisonChecking.js create mode 100644 tests/baselines/reference/spyComparisonChecking.symbols create mode 100644 tests/baselines/reference/spyComparisonChecking.types create mode 100644 tests/cases/compiler/spyComparisonChecking.ts diff --git a/tests/baselines/reference/spyComparisonChecking.errors.txt b/tests/baselines/reference/spyComparisonChecking.errors.txt new file mode 100644 index 0000000000000..8b057dd30ed2e --- /dev/null +++ b/tests/baselines/reference/spyComparisonChecking.errors.txt @@ -0,0 +1,29 @@ +tests/cases/compiler/spyComparisonChecking.ts(20,32): error TS2339: Property 'returnValue' does not exist on type 'Function'. + + +==== tests/cases/compiler/spyComparisonChecking.ts (1 errors) ==== + interface Spy { + (...params: any[]): any; + + identity: string; + and: Function; + mostRecentCall: { args: any[]; }; + argsForCall: any[]; + } + + type SpyObj = T & { + [k in keyof T]: Spy; + } + + declare function createSpyObj( + name: string, names: Array): SpyObj; + + function mock(spyName: string, methodNames: Array): SpyObj { + const spyObj = createSpyObj(spyName, methodNames); + for (const methodName of methodNames) { + spyObj[methodName].and.returnValue(1); + ~~~~~~~~~~~ +!!! error TS2339: Property 'returnValue' does not exist on type 'Function'. + } + return spyObj; + } \ No newline at end of file diff --git a/tests/baselines/reference/spyComparisonChecking.js b/tests/baselines/reference/spyComparisonChecking.js new file mode 100644 index 0000000000000..918c4e149e9a5 --- /dev/null +++ b/tests/baselines/reference/spyComparisonChecking.js @@ -0,0 +1,34 @@ +//// [spyComparisonChecking.ts] +interface Spy { + (...params: any[]): any; + + identity: string; + and: Function; + mostRecentCall: { args: any[]; }; + argsForCall: any[]; +} + +type SpyObj = T & { + [k in keyof T]: Spy; +} + +declare function createSpyObj( + name: string, names: Array): SpyObj; + +function mock(spyName: string, methodNames: Array): SpyObj { + const spyObj = createSpyObj(spyName, methodNames); + for (const methodName of methodNames) { + spyObj[methodName].and.returnValue(1); + } + return spyObj; +} + +//// [spyComparisonChecking.js] +function mock(spyName, methodNames) { + var spyObj = createSpyObj(spyName, methodNames); + for (var _i = 0, methodNames_1 = methodNames; _i < methodNames_1.length; _i++) { + var methodName = methodNames_1[_i]; + spyObj[methodName].and.returnValue(1); + } + return spyObj; +} diff --git a/tests/baselines/reference/spyComparisonChecking.symbols b/tests/baselines/reference/spyComparisonChecking.symbols new file mode 100644 index 0000000000000..50d8afaa0d13b --- /dev/null +++ b/tests/baselines/reference/spyComparisonChecking.symbols @@ -0,0 +1,75 @@ +=== tests/cases/compiler/spyComparisonChecking.ts === +interface Spy { +>Spy : Symbol(Spy, Decl(spyComparisonChecking.ts, 0, 0)) + + (...params: any[]): any; +>params : Symbol(params, Decl(spyComparisonChecking.ts, 1, 5)) + + identity: string; +>identity : Symbol(Spy.identity, Decl(spyComparisonChecking.ts, 1, 28)) + + and: Function; +>and : Symbol(Spy.and, Decl(spyComparisonChecking.ts, 3, 21)) +>Function : Symbol(Function, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + + mostRecentCall: { args: any[]; }; +>mostRecentCall : Symbol(Spy.mostRecentCall, Decl(spyComparisonChecking.ts, 4, 18)) +>args : Symbol(args, Decl(spyComparisonChecking.ts, 5, 21)) + + argsForCall: any[]; +>argsForCall : Symbol(Spy.argsForCall, Decl(spyComparisonChecking.ts, 5, 37)) +} + +type SpyObj = T & { +>SpyObj : Symbol(SpyObj, Decl(spyComparisonChecking.ts, 7, 1)) +>T : Symbol(T, Decl(spyComparisonChecking.ts, 9, 12)) +>T : Symbol(T, Decl(spyComparisonChecking.ts, 9, 12)) + + [k in keyof T]: Spy; +>k : Symbol(k, Decl(spyComparisonChecking.ts, 10, 5)) +>T : Symbol(T, Decl(spyComparisonChecking.ts, 9, 12)) +>Spy : Symbol(Spy, Decl(spyComparisonChecking.ts, 0, 0)) +} + +declare function createSpyObj( +>createSpyObj : Symbol(createSpyObj, Decl(spyComparisonChecking.ts, 11, 1)) +>T : Symbol(T, Decl(spyComparisonChecking.ts, 13, 30)) + + name: string, names: Array): SpyObj; +>name : Symbol(name, Decl(spyComparisonChecking.ts, 13, 33)) +>names : Symbol(names, Decl(spyComparisonChecking.ts, 14, 17)) +>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(spyComparisonChecking.ts, 13, 30)) +>SpyObj : Symbol(SpyObj, Decl(spyComparisonChecking.ts, 7, 1)) +>T : Symbol(T, Decl(spyComparisonChecking.ts, 13, 30)) + +function mock(spyName: string, methodNames: Array): SpyObj { +>mock : Symbol(mock, Decl(spyComparisonChecking.ts, 14, 52)) +>T : Symbol(T, Decl(spyComparisonChecking.ts, 16, 14)) +>spyName : Symbol(spyName, Decl(spyComparisonChecking.ts, 16, 17)) +>methodNames : Symbol(methodNames, Decl(spyComparisonChecking.ts, 16, 33)) +>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(spyComparisonChecking.ts, 16, 14)) +>SpyObj : Symbol(SpyObj, Decl(spyComparisonChecking.ts, 7, 1)) +>T : Symbol(T, Decl(spyComparisonChecking.ts, 16, 14)) + + const spyObj = createSpyObj(spyName, methodNames); +>spyObj : Symbol(spyObj, Decl(spyComparisonChecking.ts, 17, 9)) +>createSpyObj : Symbol(createSpyObj, Decl(spyComparisonChecking.ts, 11, 1)) +>T : Symbol(T, Decl(spyComparisonChecking.ts, 16, 14)) +>spyName : Symbol(spyName, Decl(spyComparisonChecking.ts, 16, 17)) +>methodNames : Symbol(methodNames, Decl(spyComparisonChecking.ts, 16, 33)) + + for (const methodName of methodNames) { +>methodName : Symbol(methodName, Decl(spyComparisonChecking.ts, 18, 14)) +>methodNames : Symbol(methodNames, Decl(spyComparisonChecking.ts, 16, 33)) + + spyObj[methodName].and.returnValue(1); +>spyObj[methodName].and : Symbol(Spy.and, Decl(spyComparisonChecking.ts, 3, 21)) +>spyObj : Symbol(spyObj, Decl(spyComparisonChecking.ts, 17, 9)) +>methodName : Symbol(methodName, Decl(spyComparisonChecking.ts, 18, 14)) +>and : Symbol(Spy.and, Decl(spyComparisonChecking.ts, 3, 21)) + } + return spyObj; +>spyObj : Symbol(spyObj, Decl(spyComparisonChecking.ts, 17, 9)) +} diff --git a/tests/baselines/reference/spyComparisonChecking.types b/tests/baselines/reference/spyComparisonChecking.types new file mode 100644 index 0000000000000..3401bc45ac4b3 --- /dev/null +++ b/tests/baselines/reference/spyComparisonChecking.types @@ -0,0 +1,62 @@ +=== tests/cases/compiler/spyComparisonChecking.ts === +interface Spy { + (...params: any[]): any; +>params : any[] + + identity: string; +>identity : string + + and: Function; +>and : Function + + mostRecentCall: { args: any[]; }; +>mostRecentCall : { args: any[]; } +>args : any[] + + argsForCall: any[]; +>argsForCall : any[] +} + +type SpyObj = T & { +>SpyObj : SpyObj + + [k in keyof T]: Spy; +} + +declare function createSpyObj( +>createSpyObj : (name: string, names: (keyof T)[]) => SpyObj + + name: string, names: Array): SpyObj; +>name : string +>names : (keyof T)[] + +function mock(spyName: string, methodNames: Array): SpyObj { +>mock : (spyName: string, methodNames: (keyof T)[]) => SpyObj +>spyName : string +>methodNames : (keyof T)[] + + const spyObj = createSpyObj(spyName, methodNames); +>spyObj : SpyObj +>createSpyObj(spyName, methodNames) : SpyObj +>createSpyObj : (name: string, names: (keyof T)[]) => SpyObj +>spyName : string +>methodNames : (keyof T)[] + + for (const methodName of methodNames) { +>methodName : keyof T +>methodNames : (keyof T)[] + + spyObj[methodName].and.returnValue(1); +>spyObj[methodName].and.returnValue(1) : any +>spyObj[methodName].and.returnValue : any +>spyObj[methodName].and : Function +>spyObj[methodName] : SpyObj[keyof T] +>spyObj : SpyObj +>methodName : keyof T +>and : Function +>returnValue : any +>1 : 1 + } + return spyObj; +>spyObj : SpyObj +} diff --git a/tests/cases/compiler/spyComparisonChecking.ts b/tests/cases/compiler/spyComparisonChecking.ts new file mode 100644 index 0000000000000..68b53de98db46 --- /dev/null +++ b/tests/cases/compiler/spyComparisonChecking.ts @@ -0,0 +1,23 @@ +interface Spy { + (...params: any[]): any; + + identity: string; + and: Function; + mostRecentCall: { args: any[]; }; + argsForCall: any[]; +} + +type SpyObj = T & { + [k in keyof T]: Spy; +} + +declare function createSpyObj( + name: string, names: Array): SpyObj; + +function mock(spyName: string, methodNames: Array): SpyObj { + const spyObj = createSpyObj(spyName, methodNames); + for (const methodName of methodNames) { + spyObj[methodName].and.returnValue(1); + } + return spyObj; +} \ No newline at end of file From 5bf01b32aa2b06a7e0cb542d4ddd5a641aa668b2 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Mon, 20 Aug 2018 14:23:06 -0700 Subject: [PATCH 07/12] Retain errorType when it is used as a marker for the lack of a constraint --- src/compiler/checker.ts | 36 +- ...ropertyNamesContextualType9_ES5.errors.txt | 19 +- ...ropertyNamesContextualType9_ES6.errors.txt | 19 +- .../keyofAndIndexedAccess.errors.txt | 665 ------------------ .../reference/keyofAndIndexedAccess.types | 2 +- .../reference/mappedTypeErrors2.errors.txt | 15 +- ...tiveConstraintOfIndexAccessType.errors.txt | 26 +- 7 files changed, 71 insertions(+), 711 deletions(-) delete mode 100644 tests/baselines/reference/keyofAndIndexedAccess.errors.txt diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 65cb17e1723fc..53ad912615b86 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6908,7 +6908,7 @@ namespace ts { function getConstraintOfIndexedAccess(type: IndexedAccessType) { const objectType = getBaseConstraintOfType(type.objectType) || type.objectType; const indexType = getBaseConstraintOfType(type.indexType) || type.indexType; - const constraint = !isGenericObjectType(objectType) && !isGenericIndexType(indexType) ? getIndexedAccessType(objectType, indexType) : undefined; + const constraint = !isGenericObjectType(objectType) && !isGenericIndexType(indexType) ? getIndexedAccessType(objectType, indexType, /*accessNode*/ undefined, errorType) : undefined; return constraint && constraint !== errorType ? constraint : undefined; } @@ -7059,7 +7059,7 @@ namespace ts { if (t.flags & TypeFlags.IndexedAccess) { const baseObjectType = getBaseConstraint((t).objectType); const baseIndexType = getBaseConstraint((t).indexType); - const baseIndexedAccess = baseObjectType && baseIndexType ? getIndexedAccessType(baseObjectType, baseIndexType) : undefined; + const baseIndexedAccess = baseObjectType && baseIndexType ? getIndexedAccessType(baseObjectType, baseIndexType, /*accessNode*/ undefined, errorType) : undefined; return baseIndexedAccess && baseIndexedAccess !== errorType ? getBaseConstraint(baseIndexedAccess) : undefined; } if (t.flags & TypeFlags.Conditional) { @@ -9144,7 +9144,7 @@ namespace ts { return false; } - function getPropertyTypeForIndexType(objectType: Type, indexType: Type, accessNode: ElementAccessExpression | IndexedAccessTypeNode | undefined, cacheSymbol: boolean) { + function getPropertyTypeForIndexType(objectType: Type, indexType: Type, accessNode: ElementAccessExpression | IndexedAccessTypeNode | undefined, cacheSymbol: boolean, missingType: Type) { const accessExpression = accessNode && accessNode.kind === SyntaxKind.ElementAccessExpression ? accessNode : undefined; const propName = isTypeUsableAsLateBoundName(indexType) ? getLateBoundNameFromType(indexType) : accessExpression && checkThatExpressionIsProperSymbolReference(accessExpression.argumentExpression, indexType, /*reportError*/ false) ? @@ -9157,7 +9157,7 @@ namespace ts { markPropertyAsReferenced(prop, accessExpression, /*isThisAccess*/ accessExpression.expression.kind === SyntaxKind.ThisKeyword); if (isAssignmentTarget(accessExpression) && (isReferenceToReadonlyEntity(accessExpression, prop) || isReferenceThroughNamespaceImport(accessExpression))) { error(accessExpression.argumentExpression, Diagnostics.Cannot_assign_to_0_because_it_is_a_constant_or_a_read_only_property, symbolToString(prop)); - return accessNode ? errorType : unknownType; + return missingType; } if (cacheSymbol) { getNodeLinks(accessNode!).resolvedSymbol = prop; @@ -9216,7 +9216,7 @@ namespace ts { } } } - return accessNode ? errorType : unknownType; + return missingType; } } if (isJSLiteralType(objectType)) { @@ -9237,7 +9237,7 @@ namespace ts { if (isTypeAny(indexType)) { return indexType; } - return accessNode ? errorType : unknownType; + return missingType; } function isGenericObjectType(type: Type): boolean { @@ -9290,7 +9290,7 @@ namespace ts { return instantiateType(getTemplateTypeFromMappedType(objectType), templateMapper); } - function getIndexedAccessType(objectType: Type, indexType: Type, accessNode?: ElementAccessExpression | IndexedAccessTypeNode): Type { + function getIndexedAccessType(objectType: Type, indexType: Type, accessNode?: ElementAccessExpression | IndexedAccessTypeNode, missingType = accessNode ? errorType : unknownType): Type { if (objectType === wildcardType || indexType === wildcardType) { return wildcardType; } @@ -9318,18 +9318,15 @@ namespace ts { if (indexType.flags & TypeFlags.Union && !(indexType.flags & TypeFlags.Boolean)) { const propTypes: Type[] = []; for (const t of (indexType).types) { - const propType = getPropertyTypeForIndexType(apparentObjectType, t, accessNode, /*cacheSymbol*/ false); - if (propType === unknownType) { - return unknownType; - } - if (propType === errorType) { - return errorType; + const propType = getPropertyTypeForIndexType(apparentObjectType, t, accessNode, /*cacheSymbol*/ false, missingType); + if (propType === missingType) { + return missingType; } propTypes.push(propType); } return getUnionType(propTypes); } - return getPropertyTypeForIndexType(apparentObjectType, indexType, accessNode, /*cacheSymbol*/ true); + return getPropertyTypeForIndexType(apparentObjectType, indexType, accessNode, /*cacheSymbol*/ true, missingType); } function getTypeFromIndexedAccessTypeNode(node: IndexedAccessTypeNode) { @@ -10476,8 +10473,13 @@ namespace ts { return false; } + function isOrHasGenericConditional(type: Type): boolean { + return !!(type.flags & TypeFlags.Conditional || (type.flags & TypeFlags.Intersection && some((type as IntersectionType).types, isOrHasGenericConditional))); + } + function elaborateError(node: Expression | undefined, source: Type, target: Type): boolean { if (!node) return false; + if (isOrHasGenericConditional(target)) return false; switch (node.kind) { case SyntaxKind.JsxExpression: case SyntaxKind.ParenthesizedExpression: @@ -10510,9 +10512,9 @@ namespace ts { let reportedError = false; for (let status = iterator.next(); !status.done; status = iterator.next()) { const { errorNode: prop, innerExpression: next, nameType, errorMessage } = status.value; - const sourcePropType = getIndexedAccessType(source, nameType); - const targetPropType = getIndexedAccessType(target, nameType); - if (!isTypeAssignableTo(sourcePropType, targetPropType)) { + const sourcePropType = getIndexedAccessType(source, nameType, /*accessNode*/ undefined, errorType); + const targetPropType = getIndexedAccessType(target, nameType, /*accessNode*/ undefined, errorType); + if (sourcePropType !== errorType && targetPropType !== errorType && !isTypeAssignableTo(sourcePropType, targetPropType)) { const elaborated = next && elaborateError(next, sourcePropType, targetPropType); if (elaborated) { reportedError = true; diff --git a/tests/baselines/reference/computedPropertyNamesContextualType9_ES5.errors.txt b/tests/baselines/reference/computedPropertyNamesContextualType9_ES5.errors.txt index d5fb590732513..e76b3a704dc6e 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType9_ES5.errors.txt +++ b/tests/baselines/reference/computedPropertyNamesContextualType9_ES5.errors.txt @@ -1,20 +1,21 @@ -tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES5.ts(7,5): error TS2418: Type of computed property's value is 'string', which is not assignable to type 'boolean'. -tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES5.ts(8,5): error TS2418: Type of computed property's value is 'number', which is not assignable to type 'boolean'. +tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES5.ts(6,5): error TS2322: Type '{ [x: number]: string | number; }' is not assignable to type 'I'. + Index signatures are incompatible. + Type 'string | number' is not assignable to type 'boolean'. + Type 'string' is not assignable to type 'boolean'. -==== tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES5.ts (2 errors) ==== +==== tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES5.ts (1 errors) ==== interface I { [s: string]: boolean; [s: number]: boolean; } var o: I = { + ~ +!!! error TS2322: Type '{ [x: number]: string | number; }' is not assignable to type 'I'. +!!! error TS2322: Index signatures are incompatible. +!!! error TS2322: Type 'string | number' is not assignable to type 'boolean'. +!!! error TS2322: Type 'string' is not assignable to type 'boolean'. [+"foo"]: "", - ~~~~~~~~ -!!! error TS2418: Type of computed property's value is 'string', which is not assignable to type 'boolean'. -!!! related TS6501 tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES5.ts:2:5: The expected type comes from this index signature. [+"bar"]: 0 - ~~~~~~~~ -!!! error TS2418: Type of computed property's value is 'number', which is not assignable to type 'boolean'. -!!! related TS6501 tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES5.ts:2:5: The expected type comes from this index signature. } \ No newline at end of file diff --git a/tests/baselines/reference/computedPropertyNamesContextualType9_ES6.errors.txt b/tests/baselines/reference/computedPropertyNamesContextualType9_ES6.errors.txt index 16b4ffb2bae37..468ad400c26cc 100644 --- a/tests/baselines/reference/computedPropertyNamesContextualType9_ES6.errors.txt +++ b/tests/baselines/reference/computedPropertyNamesContextualType9_ES6.errors.txt @@ -1,20 +1,21 @@ -tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES6.ts(7,5): error TS2418: Type of computed property's value is 'string', which is not assignable to type 'boolean'. -tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES6.ts(8,5): error TS2418: Type of computed property's value is 'number', which is not assignable to type 'boolean'. +tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES6.ts(6,5): error TS2322: Type '{ [x: number]: string | number; }' is not assignable to type 'I'. + Index signatures are incompatible. + Type 'string | number' is not assignable to type 'boolean'. + Type 'string' is not assignable to type 'boolean'. -==== tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES6.ts (2 errors) ==== +==== tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES6.ts (1 errors) ==== interface I { [s: string]: boolean; [s: number]: boolean; } var o: I = { + ~ +!!! error TS2322: Type '{ [x: number]: string | number; }' is not assignable to type 'I'. +!!! error TS2322: Index signatures are incompatible. +!!! error TS2322: Type 'string | number' is not assignable to type 'boolean'. +!!! error TS2322: Type 'string' is not assignable to type 'boolean'. [+"foo"]: "", - ~~~~~~~~ -!!! error TS2418: Type of computed property's value is 'string', which is not assignable to type 'boolean'. -!!! related TS6501 tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES6.ts:2:5: The expected type comes from this index signature. [+"bar"]: 0 - ~~~~~~~~ -!!! error TS2418: Type of computed property's value is 'number', which is not assignable to type 'boolean'. -!!! related TS6501 tests/cases/conformance/es6/computedProperties/computedPropertyNamesContextualType9_ES6.ts:2:5: The expected type comes from this index signature. } \ No newline at end of file diff --git a/tests/baselines/reference/keyofAndIndexedAccess.errors.txt b/tests/baselines/reference/keyofAndIndexedAccess.errors.txt deleted file mode 100644 index 41f429364463b..0000000000000 --- a/tests/baselines/reference/keyofAndIndexedAccess.errors.txt +++ /dev/null @@ -1,665 +0,0 @@ -tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts(618,33): error TS2345: Argument of type 'T[K]' is not assignable to parameter of type '{} | null | undefined'. - Type 'unknown' is not assignable to type '{} | null | undefined'. - Type 'unknown' is not assignable to type '{}'. - Type 'T[K]' is not assignable to type '{}'. - Type 'unknown' is not assignable to type '{}'. - - -==== tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts (1 errors) ==== - class Shape { - name: string; - width: number; - height: number; - visible: boolean; - } - - class TaggedShape extends Shape { - tag: string; - } - - class Item { - name: string; - price: number; - } - - class Options { - visible: "yes" | "no"; - } - - type Dictionary = { [x: string]: T }; - type NumericallyIndexed = { [x: number]: T }; - - const enum E { A, B, C } - - type K00 = keyof any; // string - type K01 = keyof string; // "toString" | "charAt" | ... - type K02 = keyof number; // "toString" | "toFixed" | "toExponential" | ... - type K03 = keyof boolean; // "valueOf" - type K04 = keyof void; // never - type K05 = keyof undefined; // never - type K06 = keyof null; // never - type K07 = keyof never; // never - - type K10 = keyof Shape; // "name" | "width" | "height" | "visible" - type K11 = keyof Shape[]; // "length" | "toString" | ... - type K12 = keyof Dictionary; // string - type K13 = keyof {}; // never - type K14 = keyof Object; // "constructor" | "toString" | ... - type K15 = keyof E; // "toString" | "toFixed" | "toExponential" | ... - type K16 = keyof [string, number]; // "0" | "1" | "length" | "toString" | ... - type K17 = keyof (Shape | Item); // "name" - type K18 = keyof (Shape & Item); // "name" | "width" | "height" | "visible" | "price" - type K19 = keyof NumericallyIndexed // never - - type KeyOf = keyof T; - - type K20 = KeyOf; // "name" | "width" | "height" | "visible" - type K21 = KeyOf>; // string - - type NAME = "name"; - type WIDTH_OR_HEIGHT = "width" | "height"; - - type Q10 = Shape["name"]; // string - type Q11 = Shape["width" | "height"]; // number - type Q12 = Shape["name" | "visible"]; // string | boolean - - type Q20 = Shape[NAME]; // string - type Q21 = Shape[WIDTH_OR_HEIGHT]; // number - - type Q30 = [string, number][0]; // string - type Q31 = [string, number][1]; // number - type Q32 = [string, number][2]; // string | number - type Q33 = [string, number][E.A]; // string - type Q34 = [string, number][E.B]; // number - type Q35 = [string, number][E.C]; // string | number - type Q36 = [string, number]["0"]; // string - type Q37 = [string, number]["1"]; // string - - type Q40 = (Shape | Options)["visible"]; // boolean | "yes" | "no" - type Q41 = (Shape & Options)["visible"]; // true & "yes" | true & "no" | false & "yes" | false & "no" - - type Q50 = Dictionary["howdy"]; // Shape - type Q51 = Dictionary[123]; // Shape - type Q52 = Dictionary[E.B]; // Shape - - declare let cond: boolean; - - function getProperty(obj: T, key: K) { - return obj[key]; - } - - function setProperty(obj: T, key: K, value: T[K]) { - obj[key] = value; - } - - function f10(shape: Shape) { - let name = getProperty(shape, "name"); // string - let widthOrHeight = getProperty(shape, cond ? "width" : "height"); // number - let nameOrVisible = getProperty(shape, cond ? "name" : "visible"); // string | boolean - setProperty(shape, "name", "rectangle"); - setProperty(shape, cond ? "width" : "height", 10); - setProperty(shape, cond ? "name" : "visible", true); // Technically not safe - } - - function f11(a: Shape[]) { - let len = getProperty(a, "length"); // number - setProperty(a, "length", len); - } - - function f12(t: [Shape, boolean]) { - let len = getProperty(t, "length"); - let s2 = getProperty(t, "0"); // Shape - let b2 = getProperty(t, "1"); // boolean - } - - function f13(foo: any, bar: any) { - let x = getProperty(foo, "x"); // any - let y = getProperty(foo, "100"); // any - let z = getProperty(foo, bar); // any - } - - class Component { - props: PropType; - getProperty(key: K) { - return this.props[key]; - } - setProperty(key: K, value: PropType[K]) { - this.props[key] = value; - } - } - - function f20(component: Component) { - let name = component.getProperty("name"); // string - let widthOrHeight = component.getProperty(cond ? "width" : "height"); // number - let nameOrVisible = component.getProperty(cond ? "name" : "visible"); // string | boolean - component.setProperty("name", "rectangle"); - component.setProperty(cond ? "width" : "height", 10) - component.setProperty(cond ? "name" : "visible", true); // Technically not safe - } - - function pluck(array: T[], key: K) { - return array.map(x => x[key]); - } - - function f30(shapes: Shape[]) { - let names = pluck(shapes, "name"); // string[] - let widths = pluck(shapes, "width"); // number[] - let nameOrVisibles = pluck(shapes, cond ? "name" : "visible"); // (string | boolean)[] - } - - function f31(key: K) { - const shape: Shape = { name: "foo", width: 5, height: 10, visible: true }; - return shape[key]; // Shape[K] - } - - function f32(key: K) { - const shape: Shape = { name: "foo", width: 5, height: 10, visible: true }; - return shape[key]; // Shape[K] - } - - function f33(shape: S, key: K) { - let name = getProperty(shape, "name"); - let prop = getProperty(shape, key); - return prop; - } - - function f34(ts: TaggedShape) { - let tag1 = f33(ts, "tag"); - let tag2 = getProperty(ts, "tag"); - } - - class C { - public x: string; - protected y: string; - private z: string; - } - - // Indexed access expressions have always permitted access to private and protected members. - // For consistency we also permit such access in indexed access types. - function f40(c: C) { - type X = C["x"]; - type Y = C["y"]; - type Z = C["z"]; - let x: X = c["x"]; - let y: Y = c["y"]; - let z: Z = c["z"]; - } - - function f50(k: keyof T, s: string) { - const x1 = s as keyof T; - const x2 = k as string; - } - - function f51(k: K, s: string) { - const x1 = s as keyof T; - const x2 = k as string; - } - - function f52(obj: { [x: string]: boolean }, k: Exclude, s: string, n: number) { - const x1 = obj[s]; - const x2 = obj[n]; - const x3 = obj[k]; - } - - function f53>(obj: { [x: string]: boolean }, k: K, s: string, n: number) { - const x1 = obj[s]; - const x2 = obj[n]; - const x3 = obj[k]; - } - - function f54(obj: T, key: keyof T) { - for (let s in obj[key]) { - } - const b = "foo" in obj[key]; - } - - function f55(obj: T, key: K) { - for (let s in obj[key]) { - } - const b = "foo" in obj[key]; - } - - function f60(source: T, target: T) { - for (let k in source) { - target[k] = source[k]; - } - } - - function f70(func: (k1: keyof (T | U), k2: keyof (T & U)) => void) { - func<{ a: any, b: any }, { a: any, c: any }>('a', 'a'); - func<{ a: any, b: any }, { a: any, c: any }>('a', 'b'); - func<{ a: any, b: any }, { a: any, c: any }>('a', 'c'); - } - - function f71(func: (x: T, y: U) => Partial) { - let x = func({ a: 1, b: "hello" }, { c: true }); - x.a; // number | undefined - x.b; // string | undefined - x.c; // boolean | undefined - } - - function f72(func: (x: T, y: U, k: K) => (T & U)[K]) { - let a = func({ a: 1, b: "hello" }, { c: true }, 'a'); // number - let b = func({ a: 1, b: "hello" }, { c: true }, 'b'); // string - let c = func({ a: 1, b: "hello" }, { c: true }, 'c'); // boolean - } - - function f73(func: (x: T, y: U, k: K) => (T & U)[K]) { - let a = func({ a: 1, b: "hello" }, { c: true }, 'a'); // number - let b = func({ a: 1, b: "hello" }, { c: true }, 'b'); // string - let c = func({ a: 1, b: "hello" }, { c: true }, 'c'); // boolean - } - - function f74(func: (x: T, y: U, k: K) => (T | U)[K]) { - let a = func({ a: 1, b: "hello" }, { a: 2, b: true }, 'a'); // number - let b = func({ a: 1, b: "hello" }, { a: 2, b: true }, 'b'); // string | boolean - } - - function f80(obj: T) { - let a1 = obj.a; // { x: any } - let a2 = obj['a']; // { x: any } - let a3 = obj['a'] as T['a']; // T["a"] - let x1 = obj.a.x; // any - let x2 = obj['a']['x']; // any - let x3 = obj['a']['x'] as T['a']['x']; // T["a"]["x"] - } - - function f81(obj: T) { - return obj['a']['x'] as T['a']['x']; - } - - function f82() { - let x1 = f81({ a: { x: "hello" } }); // string - let x2 = f81({ a: { x: 42 } }); // number - } - - function f83(obj: T, key: K) { - return obj[key]['x'] as T[K]['x']; - } - - function f84() { - let x1 = f83({ foo: { x: "hello" } }, "foo"); // string - let x2 = f83({ bar: { x: 42 } }, "bar"); // number - } - - class C1 { - x: number; - get(key: K) { - return this[key]; - } - set(key: K, value: this[K]) { - this[key] = value; - } - foo() { - let x1 = this.x; // number - let x2 = this["x"]; // number - let x3 = this.get("x"); // this["x"] - let x4 = getProperty(this, "x"); // this["x"] - this.x = 42; - this["x"] = 42; - this.set("x", 42); - setProperty(this, "x", 42); - } - } - - type S2 = { - a: string; - b: string; - }; - - function f90(x1: S2[keyof S2], x2: T[keyof S2], x3: S2[K], x4: T[K]) { - x1 = x2; - x1 = x3; - x1 = x4; - x2 = x1; - x2 = x3; - x2 = x4; - x3 = x1; - x3 = x2; - x3 = x4; - x4 = x1; - x4 = x2; - x4 = x3; - x1.length; - x2.length; - x3.length; - x4.length; - } - - function f91(x: T, y: T[keyof T], z: T[K]) { - let a: {}; - a = x; - a = y; - a = z; - } - - function f92(x: T, y: T[keyof T], z: T[K]) { - let a: {} | null | undefined; - a = x; - a = y; - a = z; - } - - // Repros from #12011 - - class Base { - get(prop: K) { - return this[prop]; - } - set(prop: K, value: this[K]) { - this[prop] = value; - } - } - - class Person extends Base { - parts: number; - constructor(parts: number) { - super(); - this.set("parts", parts); - } - getParts() { - return this.get("parts") - } - } - - class OtherPerson { - parts: number; - constructor(parts: number) { - setProperty(this, "parts", parts); - } - getParts() { - return getProperty(this, "parts") - } - } - - // Modified repro from #12544 - - function path(obj: T, key1: K1): T[K1]; - function path(obj: T, key1: K1, key2: K2): T[K1][K2]; - function path(obj: T, key1: K1, key2: K2, key3: K3): T[K1][K2][K3]; - function path(obj: any, ...keys: (string | number)[]): any; - function path(obj: any, ...keys: (string | number)[]): any { - let result = obj; - for (let k of keys) { - result = result[k]; - } - return result; - } - - type Thing = { - a: { x: number, y: string }, - b: boolean - }; - - - function f1(thing: Thing) { - let x1 = path(thing, 'a'); // { x: number, y: string } - let x2 = path(thing, 'a', 'y'); // string - let x3 = path(thing, 'b'); // boolean - let x4 = path(thing, ...['a', 'x']); // any - } - - // Repro from comment in #12114 - - const assignTo2 = (object: T, key1: K1, key2: K2) => - (value: T[K1][K2]) => object[key1][key2] = value; - - // Modified repro from #12573 - - declare function one(handler: (t: T) => void): T - var empty = one(() => {}) // inferred as {}, expected - - type Handlers = { [K in keyof T]: (t: T[K]) => void } - declare function on(handlerHash: Handlers): T - var hashOfEmpty1 = on({ test: () => {} }); // {} - var hashOfEmpty2 = on({ test: (x: boolean) => {} }); // { test: boolean } - - // Repro from #12624 - - interface Options1 { - data?: Data - computed?: Computed; - } - - declare class Component1 { - constructor(options: Options1); - get(key: K): (Data & Computed)[K]; - } - - let c1 = new Component1({ - data: { - hello: "" - } - }); - - c1.get("hello"); - - // Repro from #12625 - - interface Options2 { - data?: Data - computed?: Computed; - } - - declare class Component2 { - constructor(options: Options2); - get(key: K): (Data & Computed)[K]; - } - - // Repro from #12641 - - interface R { - p: number; - } - - function f(p: K) { - let a: any; - a[p].add; // any - } - - // Repro from #12651 - - type MethodDescriptor = { - name: string; - args: any[]; - returnValue: any; - } - - declare function dispatchMethod(name: M['name'], args: M['args']): M['returnValue']; - - type SomeMethodDescriptor = { - name: "someMethod"; - args: [string, number]; - returnValue: string[]; - } - - let result = dispatchMethod("someMethod", ["hello", 35]); - - // Repro from #13073 - - type KeyTypes = "a" | "b" - let MyThingy: { [key in KeyTypes]: string[] }; - - function addToMyThingy(key: S) { - MyThingy[key].push("a"); - } - - // Repro from #13102 - - type Handler = { - onChange: (name: keyof T) => void; - }; - - function onChangeGenericFunction(handler: Handler) { - handler.onChange('preset') - } - - // Repro from #13285 - - function updateIds, K extends string>( - obj: T, - idFields: K[], - idMapping: { [oldId: string]: string } - ): Record { - for (const idField of idFields) { - const newId = idMapping[obj[idField]]; - if (newId) { - obj[idField] = newId; - } - } - return obj; - } - - // Repro from #13285 - - function updateIds2( - obj: T, - key: K, - stringMap: { [oldId: string]: string } - ) { - var x = obj[key]; - stringMap[x]; // Should be OK. - } - - // Repro from #13514 - - declare function head>(list: T): T[0]; - - // Repro from #13604 - - class A { - props: T & { foo: string }; - } - - class B extends A<{ x: number}> { - f(p: this["props"]) { - p.x; - } - } - - // Repro from #13749 - - class Form { - private childFormFactories: {[K in keyof T]: (v: T[K]) => Form} - - public set(prop: K, value: T[K]) { - this.childFormFactories[prop](value) - } - } - - // Repro from #13787 - - class SampleClass

{ - public props: Readonly

; - constructor(props: P) { - this.props = Object.freeze(props); - } - } - - interface Foo { - foo: string; - } - - declare function merge(obj1: T, obj2: U): T & U; - - class AnotherSampleClass extends SampleClass { - constructor(props: T) { - const foo: Foo = { foo: "bar" }; - super(merge(props, foo)); - } - - public brokenMethod() { - this.props.foo.concat; - } - } - new AnotherSampleClass({}); - - // Positive repro from #17166 - function f3>(t: T, k: K, tk: T[K]): void { - for (let key in t) { - key = k // ok, K ==> keyof T - t[key] = tk; // ok, T[K] ==> T[keyof T] - } - } - - // # 21185 - type Predicates = { - [T in keyof TaggedRecord]: (variant: TaggedRecord[keyof TaggedRecord]) => variant is TaggedRecord[T] - } - - // Repros from #23592 - - type Example = { [K in keyof T]: T[K]["prop"] }; - type Result = Example<{ a: { prop: string }; b: { prop: number } }>; - - type Helper2 = { [K in keyof T]: Extract }; - type Example2 = { [K in keyof Helper2]: Helper2[K]["prop"] }; - type Result2 = Example2<{ 1: { prop: string }; 2: { prop: number } }>; - - // Repro from #23618 - - type DBBoolTable = { [k in K]: 0 | 1 } - enum Flag { - FLAG_1 = "flag_1", - FLAG_2 = "flag_2" - } - - type SimpleDBRecord = { staticField: number } & DBBoolTable - function getFlagsFromSimpleRecord(record: SimpleDBRecord, flags: Flag[]) { - return record[flags[0]]; - } - - type DynamicDBRecord = ({ dynamicField: number } | { dynamicField: string }) & DBBoolTable - function getFlagsFromDynamicRecord(record: DynamicDBRecord, flags: Flag[]) { - return record[flags[0]]; - } - - // Repro from #21368 - - interface I { - foo: string; - } - - declare function take(p: T): void; - - function fn(o: T, k: K) { - take<{} | null | undefined>(o[k]); - ~~~~ -!!! error TS2345: Argument of type 'T[K]' is not assignable to parameter of type '{} | null | undefined'. -!!! error TS2345: Type 'unknown' is not assignable to type '{} | null | undefined'. -!!! error TS2345: Type 'unknown' is not assignable to type '{}'. -!!! error TS2345: Type 'T[K]' is not assignable to type '{}'. -!!! error TS2345: Type 'unknown' is not assignable to type '{}'. - take(o[k]); - } - - // Repro from #23133 - - class Unbounded { - foo(x: T[keyof T]) { - let y: {} | undefined | null = x; - } - } - - // Repro from #23940 - - interface I7 { - x: any; - } - type Foo7 = T; - declare function f7(type: K): Foo7; - - // Repro from #21770 - - type Dict = { [key in T]: number }; - type DictDict = { [key in V]: Dict }; - - function ff1(dd: DictDict, k1: V, k2: T): number { - return dd[k1][k2]; - } - - function ff2(dd: DictDict, k1: V, k2: T): number { - const d: Dict = dd[k1]; - return d[k2]; - } - \ No newline at end of file diff --git a/tests/baselines/reference/keyofAndIndexedAccess.types b/tests/baselines/reference/keyofAndIndexedAccess.types index 8cb00c8a577fb..3434d003d54d6 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess.types +++ b/tests/baselines/reference/keyofAndIndexedAccess.types @@ -2100,7 +2100,7 @@ function fn(o: T, k: K) { >k : K take<{} | null | undefined>(o[k]); ->take<{} | null | undefined>(o[k]) : any +>take<{} | null | undefined>(o[k]) : void >take : (p: T) => void >null : null >o[k] : T[K] diff --git a/tests/baselines/reference/mappedTypeErrors2.errors.txt b/tests/baselines/reference/mappedTypeErrors2.errors.txt index 12209cd84ac17..9178731d1894d 100644 --- a/tests/baselines/reference/mappedTypeErrors2.errors.txt +++ b/tests/baselines/reference/mappedTypeErrors2.errors.txt @@ -1,15 +1,13 @@ tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(9,30): error TS2536: Type 'K' cannot be used to index type 'T1'. tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(13,30): error TS2536: Type 'K' cannot be used to index type 'T3'. +tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(15,38): error TS2536: Type 'S' cannot be used to index type '{ [key in AB[S]]: true; }'. tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(15,47): error TS2322: Type 'AB[S]' is not assignable to type 'string | number | symbol'. - Type 'unknown' is not assignable to type 'string | number | symbol'. - Type 'unknown' is not assignable to type 'symbol'. - Type 'AB[S]' is not assignable to type 'symbol'. - Type 'unknown' is not assignable to type 'symbol'. + Type 'AB[S]' is not assignable to type 'symbol'. tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(15,47): error TS2536: Type 'S' cannot be used to index type 'AB'. tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(17,49): error TS2536: Type 'L' cannot be used to index type '{ [key in AB[S]]: true; }'. -==== tests/cases/conformance/types/mapped/mappedTypeErrors2.ts (5 errors) ==== +==== tests/cases/conformance/types/mapped/mappedTypeErrors2.ts (6 errors) ==== // Repros from #17238 type AB = { @@ -29,12 +27,11 @@ tests/cases/conformance/types/mapped/mappedTypeErrors2.ts(17,49): error TS2536: !!! error TS2536: Type 'K' cannot be used to index type 'T3'. type T5 = {[key in AB[S]]: true}[S]; // Error + ~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2536: Type 'S' cannot be used to index type '{ [key in AB[S]]: true; }'. ~~~~~ !!! error TS2322: Type 'AB[S]' is not assignable to type 'string | number | symbol'. -!!! error TS2322: Type 'unknown' is not assignable to type 'string | number | symbol'. -!!! error TS2322: Type 'unknown' is not assignable to type 'symbol'. -!!! error TS2322: Type 'AB[S]' is not assignable to type 'symbol'. -!!! error TS2322: Type 'unknown' is not assignable to type 'symbol'. +!!! error TS2322: Type 'AB[S]' is not assignable to type 'symbol'. ~~~~~ !!! error TS2536: Type 'S' cannot be used to index type 'AB'. diff --git a/tests/baselines/reference/nonPrimitiveConstraintOfIndexAccessType.errors.txt b/tests/baselines/reference/nonPrimitiveConstraintOfIndexAccessType.errors.txt index 7cff0aec55d71..5784d8c46d0b4 100644 --- a/tests/baselines/reference/nonPrimitiveConstraintOfIndexAccessType.errors.txt +++ b/tests/baselines/reference/nonPrimitiveConstraintOfIndexAccessType.errors.txt @@ -1,22 +1,38 @@ +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]'. Type 'string' is not assignable to type 'never'. +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 (2 errors) ==== +==== 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; @@ -26,15 +42,23 @@ tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessTy } 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; From c68d37adc0e10c88c81a8217d603a9e6cc25b9f9 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Mon, 20 Aug 2018 14:42:24 -0700 Subject: [PATCH 08/12] Small refinement --- src/compiler/checker.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 53ad912615b86..c34da2fe157de 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -9262,11 +9262,12 @@ namespace ts { // We recursively simplify the object type as it may in turn be an indexed access type. For example, with // '{ [P in T]: { [Q in U]: number } }[T][U]' we want to first simplify the inner indexed access type. const objectType = getSimplifiedType(type.objectType); + const indexType = getSimplifiedType(type.indexType); if (objectType.flags & TypeFlags.Union) { - return type.simplified = mapType(objectType, t => getIndexedAccessType(t, type.indexType)); + return type.simplified = mapType(objectType, t => getSimplifiedType(getIndexedAccessType(t, indexType))); } if (objectType.flags & TypeFlags.Intersection) { - return type.simplified = getIntersectionType(map((objectType as IntersectionType).types, t => getIndexedAccessType(t, type.indexType))); + return type.simplified = getIntersectionType(map((objectType as IntersectionType).types, t => getSimplifiedType(getIndexedAccessType(t, indexType)))); } // If the object type is a mapped type { [P in K]: E }, where K is generic, instantiate E using a mapper // that substitutes the index type for P. For example, for an index access { [P in K]: Box }[X], we From 82b8a4f122ce7422fdb52a17ca7ef30f17b43156 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Wed, 22 Aug 2018 13:56:40 -0700 Subject: [PATCH 09/12] Add new test --- ...edAccessMethodIntersectionCanBeAccessed.js | 31 +++++++++ ...essMethodIntersectionCanBeAccessed.symbols | 69 +++++++++++++++++++ ...ccessMethodIntersectionCanBeAccessed.types | 59 ++++++++++++++++ ...edAccessMethodIntersectionCanBeAccessed.ts | 20 ++++++ 4 files changed, 179 insertions(+) create mode 100644 tests/baselines/reference/genericIndexedAccessMethodIntersectionCanBeAccessed.js create mode 100644 tests/baselines/reference/genericIndexedAccessMethodIntersectionCanBeAccessed.symbols create mode 100644 tests/baselines/reference/genericIndexedAccessMethodIntersectionCanBeAccessed.types create mode 100644 tests/cases/compiler/genericIndexedAccessMethodIntersectionCanBeAccessed.ts diff --git a/tests/baselines/reference/genericIndexedAccessMethodIntersectionCanBeAccessed.js b/tests/baselines/reference/genericIndexedAccessMethodIntersectionCanBeAccessed.js new file mode 100644 index 0000000000000..091fd6abbd0a0 --- /dev/null +++ b/tests/baselines/reference/genericIndexedAccessMethodIntersectionCanBeAccessed.js @@ -0,0 +1,31 @@ +//// [genericIndexedAccessMethodIntersectionCanBeAccessed.ts] +type ExtendedService = { + [K in keyof T]: T[K] & { + __$daemonMode?: string; + __$action?: string; + }; +}; + +type Service = { + [K in keyof T]: T[K] & {id?: string}; +}; + +export const createService = ( + ServiceCtr: ExtendedService & Service +) => { + Object.keys(ServiceCtr).forEach(key => { + const method = (ServiceCtr)[key as keyof T]; + const {__$daemonMode, __$action, id} = method; + }) +} + + +//// [genericIndexedAccessMethodIntersectionCanBeAccessed.js] +"use strict"; +exports.__esModule = true; +exports.createService = function (ServiceCtr) { + Object.keys(ServiceCtr).forEach(function (key) { + var method = (ServiceCtr)[key]; + var __$daemonMode = method.__$daemonMode, __$action = method.__$action, id = method.id; + }); +}; diff --git a/tests/baselines/reference/genericIndexedAccessMethodIntersectionCanBeAccessed.symbols b/tests/baselines/reference/genericIndexedAccessMethodIntersectionCanBeAccessed.symbols new file mode 100644 index 0000000000000..2b619c997dc54 --- /dev/null +++ b/tests/baselines/reference/genericIndexedAccessMethodIntersectionCanBeAccessed.symbols @@ -0,0 +1,69 @@ +=== tests/cases/compiler/genericIndexedAccessMethodIntersectionCanBeAccessed.ts === +type ExtendedService = { +>ExtendedService : Symbol(ExtendedService, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 0, 0)) +>T : Symbol(T, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 0, 21)) + + [K in keyof T]: T[K] & { +>K : Symbol(K, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 1, 5)) +>T : Symbol(T, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 0, 21)) +>T : Symbol(T, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 0, 21)) +>K : Symbol(K, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 1, 5)) + + __$daemonMode?: string; +>__$daemonMode : Symbol(__$daemonMode, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 1, 28)) + + __$action?: string; +>__$action : Symbol(__$action, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 2, 31)) + + }; +}; + +type Service = { +>Service : Symbol(Service, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 5, 2)) +>T : Symbol(T, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 7, 13)) + + [K in keyof T]: T[K] & {id?: string}; +>K : Symbol(K, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 8, 5)) +>T : Symbol(T, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 7, 13)) +>T : Symbol(T, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 7, 13)) +>K : Symbol(K, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 8, 5)) +>id : Symbol(id, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 8, 28)) + +}; + +export const createService = ( +>createService : Symbol(createService, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 11, 12)) +>T : Symbol(T, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 11, 30)) + + ServiceCtr: ExtendedService & Service +>ServiceCtr : Symbol(ServiceCtr, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 11, 33)) +>ExtendedService : Symbol(ExtendedService, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 0, 0)) +>T : Symbol(T, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 11, 30)) +>Service : Symbol(Service, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 5, 2)) +>T : Symbol(T, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 11, 30)) + +) => { + Object.keys(ServiceCtr).forEach(key => { +>Object.keys(ServiceCtr).forEach : Symbol(Array.forEach, Decl(lib.es5.d.ts, --, --)) +>Object.keys : Symbol(ObjectConstructor.keys, Decl(lib.es5.d.ts, --, --)) +>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +>keys : Symbol(ObjectConstructor.keys, Decl(lib.es5.d.ts, --, --)) +>ServiceCtr : Symbol(ServiceCtr, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 11, 33)) +>forEach : Symbol(Array.forEach, Decl(lib.es5.d.ts, --, --)) +>key : Symbol(key, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 14, 36)) + + const method = (ServiceCtr)[key as keyof T]; +>method : Symbol(method, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 15, 13)) +>ServiceCtr : Symbol(ServiceCtr, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 11, 33)) +>key : Symbol(key, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 14, 36)) +>T : Symbol(T, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 11, 30)) + + const {__$daemonMode, __$action, id} = method; +>__$daemonMode : Symbol(__$daemonMode, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 16, 15)) +>__$action : Symbol(__$action, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 16, 29)) +>id : Symbol(id, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 16, 40)) +>method : Symbol(method, Decl(genericIndexedAccessMethodIntersectionCanBeAccessed.ts, 15, 13)) + + }) +} + diff --git a/tests/baselines/reference/genericIndexedAccessMethodIntersectionCanBeAccessed.types b/tests/baselines/reference/genericIndexedAccessMethodIntersectionCanBeAccessed.types new file mode 100644 index 0000000000000..d16a75912f491 --- /dev/null +++ b/tests/baselines/reference/genericIndexedAccessMethodIntersectionCanBeAccessed.types @@ -0,0 +1,59 @@ +=== tests/cases/compiler/genericIndexedAccessMethodIntersectionCanBeAccessed.ts === +type ExtendedService = { +>ExtendedService : ExtendedService + + [K in keyof T]: T[K] & { + __$daemonMode?: string; +>__$daemonMode : string | undefined + + __$action?: string; +>__$action : string | undefined + + }; +}; + +type Service = { +>Service : Service + + [K in keyof T]: T[K] & {id?: string}; +>id : string | undefined + +}; + +export const createService = ( +>createService : (ServiceCtr: ExtendedService & Service) => void +>( ServiceCtr: ExtendedService & Service) => { Object.keys(ServiceCtr).forEach(key => { const method = (ServiceCtr)[key as keyof T]; const {__$daemonMode, __$action, id} = method; })} : (ServiceCtr: ExtendedService & Service) => void + + ServiceCtr: ExtendedService & Service +>ServiceCtr : ExtendedService & Service + +) => { + Object.keys(ServiceCtr).forEach(key => { +>Object.keys(ServiceCtr).forEach(key => { const method = (ServiceCtr)[key as keyof T]; const {__$daemonMode, __$action, id} = method; }) : void +>Object.keys(ServiceCtr).forEach : (callbackfn: (value: string, index: number, array: string[]) => void, thisArg?: any) => void +>Object.keys(ServiceCtr) : string[] +>Object.keys : (o: {}) => string[] +>Object : ObjectConstructor +>keys : (o: {}) => string[] +>ServiceCtr : ExtendedService & Service +>forEach : (callbackfn: (value: string, index: number, array: string[]) => void, thisArg?: any) => void +>key => { const method = (ServiceCtr)[key as keyof T]; const {__$daemonMode, __$action, id} = method; } : (key: string) => void +>key : string + + const method = (ServiceCtr)[key as keyof T]; +>method : (ExtendedService & Service)[keyof T] +>(ServiceCtr)[key as keyof T] : (ExtendedService & Service)[keyof T] +>(ServiceCtr) : ExtendedService & Service +>ServiceCtr : ExtendedService & Service +>key as keyof T : keyof T +>key : string + + const {__$daemonMode, __$action, id} = method; +>__$daemonMode : string | undefined +>__$action : string | undefined +>id : string | undefined +>method : (ExtendedService & Service)[keyof T] + + }) +} + diff --git a/tests/cases/compiler/genericIndexedAccessMethodIntersectionCanBeAccessed.ts b/tests/cases/compiler/genericIndexedAccessMethodIntersectionCanBeAccessed.ts new file mode 100644 index 0000000000000..b4d604ff90aec --- /dev/null +++ b/tests/cases/compiler/genericIndexedAccessMethodIntersectionCanBeAccessed.ts @@ -0,0 +1,20 @@ +// @strict: true +type ExtendedService = { + [K in keyof T]: T[K] & { + __$daemonMode?: string; + __$action?: string; + }; +}; + +type Service = { + [K in keyof T]: T[K] & {id?: string}; +}; + +export const createService = ( + ServiceCtr: ExtendedService & Service +) => { + Object.keys(ServiceCtr).forEach(key => { + const method = (ServiceCtr)[key as keyof T]; + const {__$daemonMode, __$action, id} = method; + }) +} From 98e9b3e08215104a120c1cdcefb173d904cc2ccb Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Wed, 22 Aug 2018 17:26:20 -0700 Subject: [PATCH 10/12] Move most potentially recursive types from isIdenticalTo into structuredTypeRelatedTo to match the non-identity relations --- src/compiler/checker.ts | 59 ++++--- ...larlySimplifyingConditionalTypesNoCrash.js | 55 +++++++ ...SimplifyingConditionalTypesNoCrash.symbols | 154 ++++++++++++++++++ ...lySimplifyingConditionalTypesNoCrash.types | 92 +++++++++++ ...larlySimplifyingConditionalTypesNoCrash.ts | 48 ++++++ 5 files changed, 381 insertions(+), 27 deletions(-) create mode 100644 tests/baselines/reference/circularlySimplifyingConditionalTypesNoCrash.js create mode 100644 tests/baselines/reference/circularlySimplifyingConditionalTypesNoCrash.symbols create mode 100644 tests/baselines/reference/circularlySimplifyingConditionalTypesNoCrash.types create mode 100644 tests/cases/compiler/circularlySimplifyingConditionalTypesNoCrash.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5b4d829b0b4a9..2ee08cbf8d065 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11256,7 +11256,7 @@ namespace ts { function isIdenticalTo(source: Type, target: Type): Ternary { let result: Ternary; const flags = source.flags & target.flags; - if (flags & TypeFlags.Object) { + if (flags & TypeFlags.Object || flags & TypeFlags.IndexedAccess || flags & TypeFlags.Conditional || flags & TypeFlags.Index || flags & TypeFlags.Substitution) { return recursiveTypeRelatedTo(source, target, /*reportErrors*/ false); } if (flags & (TypeFlags.Union | TypeFlags.Intersection)) { @@ -11266,32 +11266,6 @@ namespace ts { } } } - if (flags & TypeFlags.Index) { - return isRelatedTo((source).type, (target).type, /*reportErrors*/ false); - } - if (flags & TypeFlags.IndexedAccess) { - if (result = isRelatedTo((source).objectType, (target).objectType, /*reportErrors*/ false)) { - if (result &= isRelatedTo((source).indexType, (target).indexType, /*reportErrors*/ false)) { - return result; - } - } - } - if (flags & TypeFlags.Conditional) { - if ((source).root.isDistributive === (target).root.isDistributive) { - if (result = isRelatedTo((source).checkType, (target).checkType, /*reportErrors*/ false)) { - if (result &= isRelatedTo((source).extendsType, (target).extendsType, /*reportErrors*/ false)) { - if (result &= isRelatedTo(getTrueTypeFromConditionalType(source), getTrueTypeFromConditionalType(target), /*reportErrors*/ false)) { - if (result &= isRelatedTo(getFalseTypeFromConditionalType(source), getFalseTypeFromConditionalType(target), /*reportErrors*/ false)) { - return result; - } - } - } - } - } - } - if (flags & TypeFlags.Substitution) { - return isRelatedTo((source).substitute, (target).substitute, /*reportErrors*/ false); - } return Ternary.False; } @@ -11603,6 +11577,37 @@ namespace ts { } function structuredTypeRelatedTo(source: Type, target: Type, reportErrors: boolean): Ternary { + const flags = source.flags & target.flags; + if (relation === identityRelation && !(flags & TypeFlags.Object)) { + if (flags & TypeFlags.Index) { + return isRelatedTo((source).type, (target).type, /*reportErrors*/ false); + } + let result = Ternary.False; + if (flags & TypeFlags.IndexedAccess) { + if (result = isRelatedTo((source).objectType, (target).objectType, /*reportErrors*/ false)) { + if (result &= isRelatedTo((source).indexType, (target).indexType, /*reportErrors*/ false)) { + return result; + } + } + } + if (flags & TypeFlags.Conditional) { + if ((source).root.isDistributive === (target).root.isDistributive) { + if (result = isRelatedTo((source).checkType, (target).checkType, /*reportErrors*/ false)) { + if (result &= isRelatedTo((source).extendsType, (target).extendsType, /*reportErrors*/ false)) { + if (result &= isRelatedTo(getTrueTypeFromConditionalType(source), getTrueTypeFromConditionalType(target), /*reportErrors*/ false)) { + if (result &= isRelatedTo(getFalseTypeFromConditionalType(source), getFalseTypeFromConditionalType(target), /*reportErrors*/ false)) { + return result; + } + } + } + } + } + } + if (flags & TypeFlags.Substitution) { + return isRelatedTo((source).substitute, (target).substitute, /*reportErrors*/ false); + } + return Ternary.False; + } let result: Ternary; let originalErrorInfo: DiagnosticMessageChain | undefined; const saveErrorInfo = errorInfo; diff --git a/tests/baselines/reference/circularlySimplifyingConditionalTypesNoCrash.js b/tests/baselines/reference/circularlySimplifyingConditionalTypesNoCrash.js new file mode 100644 index 0000000000000..724a8f8dbeadb --- /dev/null +++ b/tests/baselines/reference/circularlySimplifyingConditionalTypesNoCrash.js @@ -0,0 +1,55 @@ +//// [circularlySimplifyingConditionalTypesNoCrash.ts] +type Omit = Pick>; + +type Shared< // Circularly self constraining type, defered thanks to mapping + InjectedProps, + DecorationTargetProps extends Shared + > = { + [P in Extract]: InjectedProps[P] extends DecorationTargetProps[P] ? DecorationTargetProps[P] : never; + }; + +interface ComponentClass

{ + defaultProps?: Partial

; // Inference target is also mapped _and_ optional +} + +interface InferableComponentEnhancerWithProps { +

>( + component: ComponentClass

+ ): ComponentClass> & TNeedsProps> & { WrappedComponent: ComponentClass

} +} // Then intersected with and indexed via Omit and & + +interface Connect { // Then strictly compared with another signature in its context + ( + mapStateToProps: unknown, + ): InferableComponentEnhancerWithProps; + + ( + mapStateToProps: null | undefined, + mapDispatchToProps: unknown, + mergeProps: null | undefined, + options: unknown + ): InferableComponentEnhancerWithProps; +} + +declare var connect: Connect; + +const myStoreConnect: Connect = function( + mapStateToProps?: any, + mapDispatchToProps?: any, + mergeProps?: any, + options: unknown = {}, +) { + return connect( + mapStateToProps, + mapDispatchToProps, + mergeProps, + options, + ); +}; + +//// [circularlySimplifyingConditionalTypesNoCrash.js] +"use strict"; +var myStoreConnect = function (mapStateToProps, mapDispatchToProps, mergeProps, options) { + if (options === void 0) { options = {}; } + return connect(mapStateToProps, mapDispatchToProps, mergeProps, options); +}; diff --git a/tests/baselines/reference/circularlySimplifyingConditionalTypesNoCrash.symbols b/tests/baselines/reference/circularlySimplifyingConditionalTypesNoCrash.symbols new file mode 100644 index 0000000000000..97abdadf2b39e --- /dev/null +++ b/tests/baselines/reference/circularlySimplifyingConditionalTypesNoCrash.symbols @@ -0,0 +1,154 @@ +=== tests/cases/compiler/circularlySimplifyingConditionalTypesNoCrash.ts === +type Omit = Pick>; +>Omit : Symbol(Omit, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 0, 0)) +>T : Symbol(T, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 0, 10)) +>K : Symbol(K, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 0, 12)) +>T : Symbol(T, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 0, 10)) +>Pick : Symbol(Pick, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 0, 10)) +>Exclude : Symbol(Exclude, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 0, 10)) +>K : Symbol(K, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 0, 12)) + +type Shared< // Circularly self constraining type, defered thanks to mapping +>Shared : Symbol(Shared, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 0, 63)) + + InjectedProps, +>InjectedProps : Symbol(InjectedProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 2, 12)) + + DecorationTargetProps extends Shared +>DecorationTargetProps : Symbol(DecorationTargetProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 3, 18)) +>Shared : Symbol(Shared, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 0, 63)) +>InjectedProps : Symbol(InjectedProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 2, 12)) +>DecorationTargetProps : Symbol(DecorationTargetProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 3, 18)) + + > = { + [P in Extract]: InjectedProps[P] extends DecorationTargetProps[P] ? DecorationTargetProps[P] : never; +>P : Symbol(P, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 6, 9)) +>Extract : Symbol(Extract, Decl(lib.es5.d.ts, --, --)) +>InjectedProps : Symbol(InjectedProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 2, 12)) +>DecorationTargetProps : Symbol(DecorationTargetProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 3, 18)) +>InjectedProps : Symbol(InjectedProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 2, 12)) +>P : Symbol(P, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 6, 9)) +>DecorationTargetProps : Symbol(DecorationTargetProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 3, 18)) +>P : Symbol(P, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 6, 9)) +>DecorationTargetProps : Symbol(DecorationTargetProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 3, 18)) +>P : Symbol(P, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 6, 9)) + + }; + +interface ComponentClass

{ +>ComponentClass : Symbol(ComponentClass, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 7, 6)) +>P : Symbol(P, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 9, 25)) + + defaultProps?: Partial

; // Inference target is also mapped _and_ optional +>defaultProps : Symbol(ComponentClass.defaultProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 9, 29)) +>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --)) +>P : Symbol(P, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 9, 25)) +} + +interface InferableComponentEnhancerWithProps { +>InferableComponentEnhancerWithProps : Symbol(InferableComponentEnhancerWithProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 11, 1)) +>TInjectedProps : Symbol(TInjectedProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 13, 46)) +>TNeedsProps : Symbol(TNeedsProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 13, 61)) + +

>( +>P : Symbol(P, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 14, 5)) +>Shared : Symbol(Shared, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 0, 63)) +>TInjectedProps : Symbol(TInjectedProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 13, 46)) +>P : Symbol(P, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 14, 5)) + + component: ComponentClass

+>component : Symbol(component, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 14, 42)) +>ComponentClass : Symbol(ComponentClass, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 7, 6)) +>P : Symbol(P, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 14, 5)) + + ): ComponentClass> & TNeedsProps> & { WrappedComponent: ComponentClass

} +>ComponentClass : Symbol(ComponentClass, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 7, 6)) +>Omit : Symbol(Omit, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 0, 0)) +>P : Symbol(P, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 14, 5)) +>Shared : Symbol(Shared, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 0, 63)) +>TInjectedProps : Symbol(TInjectedProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 13, 46)) +>P : Symbol(P, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 14, 5)) +>TNeedsProps : Symbol(TNeedsProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 13, 61)) +>WrappedComponent : Symbol(WrappedComponent, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 16, 81)) +>ComponentClass : Symbol(ComponentClass, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 7, 6)) +>P : Symbol(P, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 14, 5)) + +} // Then intersected with and indexed via Omit and & + +interface Connect { // Then strictly compared with another signature in its context +>Connect : Symbol(Connect, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 17, 1)) + + ( +>TStateProps : Symbol(TStateProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 20, 5)) +>TOwnProps : Symbol(TOwnProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 20, 17)) + + mapStateToProps: unknown, +>mapStateToProps : Symbol(mapStateToProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 20, 29)) + + ): InferableComponentEnhancerWithProps; +>InferableComponentEnhancerWithProps : Symbol(InferableComponentEnhancerWithProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 11, 1)) +>TStateProps : Symbol(TStateProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 20, 5)) +>TOwnProps : Symbol(TOwnProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 20, 17)) + + ( +>TDispatchProps : Symbol(TDispatchProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 24, 5)) +>TOwnProps : Symbol(TOwnProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 24, 20)) + + mapStateToProps: null | undefined, +>mapStateToProps : Symbol(mapStateToProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 24, 32)) + + mapDispatchToProps: unknown, +>mapDispatchToProps : Symbol(mapDispatchToProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 25, 42)) + + mergeProps: null | undefined, +>mergeProps : Symbol(mergeProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 26, 36)) + + options: unknown +>options : Symbol(options, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 27, 37)) + + ): InferableComponentEnhancerWithProps; +>InferableComponentEnhancerWithProps : Symbol(InferableComponentEnhancerWithProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 11, 1)) +>TDispatchProps : Symbol(TDispatchProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 24, 5)) +>TOwnProps : Symbol(TOwnProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 24, 20)) +} + +declare var connect: Connect; +>connect : Symbol(connect, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 32, 11)) +>Connect : Symbol(Connect, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 17, 1)) + +const myStoreConnect: Connect = function( +>myStoreConnect : Symbol(myStoreConnect, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 34, 5)) +>Connect : Symbol(Connect, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 17, 1)) + + mapStateToProps?: any, +>mapStateToProps : Symbol(mapStateToProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 34, 41)) + + mapDispatchToProps?: any, +>mapDispatchToProps : Symbol(mapDispatchToProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 35, 26)) + + mergeProps?: any, +>mergeProps : Symbol(mergeProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 36, 29)) + + options: unknown = {}, +>options : Symbol(options, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 37, 21)) + +) { + return connect( +>connect : Symbol(connect, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 32, 11)) + + mapStateToProps, +>mapStateToProps : Symbol(mapStateToProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 34, 41)) + + mapDispatchToProps, +>mapDispatchToProps : Symbol(mapDispatchToProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 35, 26)) + + mergeProps, +>mergeProps : Symbol(mergeProps, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 36, 29)) + + options, +>options : Symbol(options, Decl(circularlySimplifyingConditionalTypesNoCrash.ts, 37, 21)) + + ); +}; diff --git a/tests/baselines/reference/circularlySimplifyingConditionalTypesNoCrash.types b/tests/baselines/reference/circularlySimplifyingConditionalTypesNoCrash.types new file mode 100644 index 0000000000000..4a3a1fdbe53aa --- /dev/null +++ b/tests/baselines/reference/circularlySimplifyingConditionalTypesNoCrash.types @@ -0,0 +1,92 @@ +=== tests/cases/compiler/circularlySimplifyingConditionalTypesNoCrash.ts === +type Omit = Pick>; +>Omit : Pick> + +type Shared< // Circularly self constraining type, defered thanks to mapping +>Shared : Shared + + InjectedProps, + DecorationTargetProps extends Shared + > = { + [P in Extract]: InjectedProps[P] extends DecorationTargetProps[P] ? DecorationTargetProps[P] : never; + }; + +interface ComponentClass

{ + defaultProps?: Partial

; // Inference target is also mapped _and_ optional +>defaultProps : Partial

| undefined +} + +interface InferableComponentEnhancerWithProps { +

>( + component: ComponentClass

+>component : ComponentClass

+ + ): ComponentClass> & TNeedsProps> & { WrappedComponent: ComponentClass

} +>WrappedComponent : ComponentClass

+ +} // Then intersected with and indexed via Omit and & + +interface Connect { // Then strictly compared with another signature in its context + ( + mapStateToProps: unknown, +>mapStateToProps : unknown + + ): InferableComponentEnhancerWithProps; + + ( + mapStateToProps: null | undefined, +>mapStateToProps : null | undefined +>null : null + + mapDispatchToProps: unknown, +>mapDispatchToProps : unknown + + mergeProps: null | undefined, +>mergeProps : null | undefined +>null : null + + options: unknown +>options : unknown + + ): InferableComponentEnhancerWithProps; +} + +declare var connect: Connect; +>connect : Connect + +const myStoreConnect: Connect = function( +>myStoreConnect : Connect +>function( mapStateToProps?: any, mapDispatchToProps?: any, mergeProps?: any, options: unknown = {},) { return connect( mapStateToProps, mapDispatchToProps, mergeProps, options, );} : (mapStateToProps?: any, mapDispatchToProps?: any, mergeProps?: any, options?: unknown) => InferableComponentEnhancerWithProps<{}, {}> + + mapStateToProps?: any, +>mapStateToProps : any + + mapDispatchToProps?: any, +>mapDispatchToProps : any + + mergeProps?: any, +>mergeProps : any + + options: unknown = {}, +>options : unknown +>{} : {} + +) { + return connect( +>connect( mapStateToProps, mapDispatchToProps, mergeProps, options, ) : InferableComponentEnhancerWithProps<{}, {}> +>connect : Connect + + mapStateToProps, +>mapStateToProps : any + + mapDispatchToProps, +>mapDispatchToProps : any + + mergeProps, +>mergeProps : any + + options, +>options : unknown + + ); +}; diff --git a/tests/cases/compiler/circularlySimplifyingConditionalTypesNoCrash.ts b/tests/cases/compiler/circularlySimplifyingConditionalTypesNoCrash.ts new file mode 100644 index 0000000000000..ccacb10dd2d64 --- /dev/null +++ b/tests/cases/compiler/circularlySimplifyingConditionalTypesNoCrash.ts @@ -0,0 +1,48 @@ +// @strict: true +type Omit = Pick>; + +type Shared< // Circularly self constraining type, defered thanks to mapping + InjectedProps, + DecorationTargetProps extends Shared + > = { + [P in Extract]: InjectedProps[P] extends DecorationTargetProps[P] ? DecorationTargetProps[P] : never; + }; + +interface ComponentClass

{ + defaultProps?: Partial

; // Inference target is also mapped _and_ optional +} + +interface InferableComponentEnhancerWithProps { +

>( + component: ComponentClass

+ ): ComponentClass> & TNeedsProps> & { WrappedComponent: ComponentClass

} +} // Then intersected with and indexed via Omit and & + +interface Connect { // Then strictly compared with another signature in its context + ( + mapStateToProps: unknown, + ): InferableComponentEnhancerWithProps; + + ( + mapStateToProps: null | undefined, + mapDispatchToProps: unknown, + mergeProps: null | undefined, + options: unknown + ): InferableComponentEnhancerWithProps; +} + +declare var connect: Connect; + +const myStoreConnect: Connect = function( + mapStateToProps?: any, + mapDispatchToProps?: any, + mergeProps?: any, + options: unknown = {}, +) { + return connect( + mapStateToProps, + mapDispatchToProps, + mergeProps, + options, + ); +}; \ No newline at end of file From 22b9a16c46413bc997576207559bff1d6c13bcd0 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Mon, 27 Aug 2018 10:13:35 -0700 Subject: [PATCH 11/12] Fix nits --- src/compiler/checker.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2ee08cbf8d065..daf9cee2085b3 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10501,8 +10501,7 @@ namespace ts { } function elaborateError(node: Expression | undefined, source: Type, target: Type): boolean { - if (!node) return false; - if (isOrHasGenericConditional(target)) return false; + if (!node || isOrHasGenericConditional(target)) return false; switch (node.kind) { case SyntaxKind.JsxExpression: case SyntaxKind.ParenthesizedExpression: @@ -15141,9 +15140,9 @@ namespace ts { return type.flags & TypeFlags.InstantiableNonPrimitive && maybeTypeOfKind(getBaseConstraintOfType(type) || emptyObjectType, TypeFlags.Nullable); } - function getConstraintForLocation(type: Type, node?: Node): Type; - function getConstraintForLocation(type: Type | undefined, node?: Node): Type | undefined; - function getConstraintForLocation(type: Type, node?: Node): Type | undefined { + function getConstraintForLocation(type: Type, node: Node | undefined): Type; + function getConstraintForLocation(type: Type | undefined, node: Node | undefined): Type | undefined; + function getConstraintForLocation(type: Type, node: Node | undefined): Type | undefined { // When a node is the left hand expression of a property access, element access, or call expression, // and the type of the node includes type variables with constraints that are nullable, we fetch the // apparent type of the node *before* performing control flow analysis such that narrowings apply to From 37e5f6de44e0b2f4e716a57847af5399c1606779 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Mon, 27 Aug 2018 10:25:23 -0700 Subject: [PATCH 12/12] Doesnt need to be undefineable at all --- src/compiler/checker.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index daf9cee2085b3..b53ec9febb0a5 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -15140,14 +15140,14 @@ namespace ts { return type.flags & TypeFlags.InstantiableNonPrimitive && maybeTypeOfKind(getBaseConstraintOfType(type) || emptyObjectType, TypeFlags.Nullable); } - function getConstraintForLocation(type: Type, node: Node | undefined): Type; - function getConstraintForLocation(type: Type | undefined, node: Node | undefined): Type | undefined; - function getConstraintForLocation(type: Type, node: Node | undefined): Type | undefined { + function getConstraintForLocation(type: Type, node: Node): Type; + function getConstraintForLocation(type: Type | undefined, node: Node): Type | undefined; + function getConstraintForLocation(type: Type, node: Node): Type | undefined { // When a node is the left hand expression of a property access, element access, or call expression, // and the type of the node includes type variables with constraints that are nullable, we fetch the // apparent type of the node *before* performing control flow analysis such that narrowings apply to // the constraint type. - if (node && type && isConstraintPosition(node) && forEachType(type, typeHasNullableConstraint)) { + if (type && isConstraintPosition(node) && forEachType(type, typeHasNullableConstraint)) { return mapType(getWidenedType(type), getBaseConstraintOrType); } return type;