diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 9a64dce125591..9f5673af3eee9 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10196,7 +10196,6 @@ namespace ts { function getSimplifiedType(type: Type, writing: boolean): Type { return type.flags & TypeFlags.IndexedAccess ? getSimplifiedIndexedAccessType(type, writing) : type.flags & TypeFlags.Conditional ? getSimplifiedConditionalType(type, writing) : - type.flags & TypeFlags.Substitution ? writing ? (type).typeVariable : (type).substitute : type; } @@ -12290,7 +12289,7 @@ namespace ts { let depth = 0; let expandingFlags = ExpandingFlags.None; let overflow = false; - let suppressNextError = false; + let overrideNextErrorInfo: DiagnosticMessageChain | undefined; Debug.assert(relation !== identityRelation || !errorNode, "no error reporting in identity checking"); @@ -12432,6 +12431,12 @@ namespace ts { if (isFreshLiteralType(target)) { target = (target).regularType; } + if (source.flags & TypeFlags.Substitution) { + source = (source).substitute; + } + if (target.flags & TypeFlags.Substitution) { + target = (target).typeVariable; + } if (source.flags & TypeFlags.Simplifiable) { source = getSimplifiedType(source, /*writing*/ false); } @@ -12571,10 +12576,14 @@ namespace ts { } if (!result && reportErrors) { - const maybeSuppress = suppressNextError; - suppressNextError = false; + let maybeSuppress = overrideNextErrorInfo; + overrideNextErrorInfo = undefined; if (source.flags & TypeFlags.Object && target.flags & TypeFlags.Object) { + const currentError = errorInfo; tryElaborateArrayLikeErrors(source, target, reportErrors); + if (errorInfo !== currentError) { + maybeSuppress = errorInfo; + } } if (source.flags & TypeFlags.Object && target.flags & TypeFlags.Primitive) { tryElaborateErrorsForPrimitivesAndObjects(source, target); @@ -13506,9 +13515,10 @@ namespace ts { if (unmatchedProperty) { if (reportErrors) { const props = arrayFrom(getUnmatchedProperties(source, target, requireOptionalProperties, /*matchDiscriminantProperties*/ false)); + let shouldSkipElaboration = false; if (!headMessage || (headMessage.code !== Diagnostics.Class_0_incorrectly_implements_interface_1.code && headMessage.code !== Diagnostics.Class_0_incorrectly_implements_class_1_Did_you_mean_to_extend_1_and_inherit_its_members_as_a_subclass.code)) { - suppressNextError = true; // Retain top-level error for interface implementing issues, otherwise omit it + shouldSkipElaboration = true; // Retain top-level error for interface implementing issues, otherwise omit it } if (props.length === 1) { const propName = symbolToString(unmatchedProperty); @@ -13516,6 +13526,9 @@ namespace ts { if (length(unmatchedProperty.declarations)) { associateRelatedInfo(createDiagnosticForNode(unmatchedProperty.declarations[0], Diagnostics._0_is_declared_here, propName)); } + if (shouldSkipElaboration) { + overrideNextErrorInfo = errorInfo; + } } else if (tryElaborateArrayLikeErrors(source, target, /*reportErrors*/ false)) { if (props.length > 5) { // arbitrary cutoff for too-long list form @@ -13524,7 +13537,11 @@ namespace ts { else { reportError(Diagnostics.Type_0_is_missing_the_following_properties_from_type_1_Colon_2, typeToString(source), typeToString(target), map(props, p => symbolToString(p)).join(", ")); } + if (shouldSkipElaboration) { + overrideNextErrorInfo = errorInfo; + } } + // ELSE: No array like or unmatched property error - just issue top level error (errorInfo = undefined) } return Ternary.False; } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 75fb702609b70..db1a62937e662 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -3967,7 +3967,7 @@ namespace ts { /* @internal */ ObjectFlagsType = Nullable | Never | Object | Union | Intersection, /* @internal */ - Simplifiable = IndexedAccess | Conditional | Substitution, + Simplifiable = IndexedAccess | Conditional, // 'Narrowable' types are types where narrowing actually narrows. // This *should* be every type other than null, undefined, void, and never Narrowable = Any | Unknown | StructuredOrInstantiable | StringLike | NumberLike | BigIntLike | BooleanLike | ESSymbol | UniqueESSymbol | NonPrimitive, diff --git a/src/services/services.ts b/src/services/services.ts index 42d2278bcba93..628ec395437b6 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1467,33 +1467,41 @@ namespace ts { } const typeChecker = program.getTypeChecker(); - const symbol = getSymbolAtLocationForQuickInfo(node, typeChecker); + const nodeForQuickInfo = getNodeForQuickInfo(node); + const symbol = getSymbolAtLocationForQuickInfo(nodeForQuickInfo, typeChecker); if (!symbol || typeChecker.isUnknownSymbol(symbol)) { - const type = shouldGetType(sourceFile, node, position) ? typeChecker.getTypeAtLocation(node) : undefined; + const type = shouldGetType(sourceFile, nodeForQuickInfo, position) ? typeChecker.getTypeAtLocation(nodeForQuickInfo) : undefined; return type && { kind: ScriptElementKind.unknown, kindModifiers: ScriptElementKindModifier.none, - textSpan: createTextSpanFromNode(node, sourceFile), - displayParts: typeChecker.runWithCancellationToken(cancellationToken, typeChecker => typeToDisplayParts(typeChecker, type, getContainerNode(node))), + textSpan: createTextSpanFromNode(nodeForQuickInfo, sourceFile), + displayParts: typeChecker.runWithCancellationToken(cancellationToken, typeChecker => typeToDisplayParts(typeChecker, type, getContainerNode(nodeForQuickInfo))), documentation: type.symbol ? type.symbol.getDocumentationComment(typeChecker) : undefined, tags: type.symbol ? type.symbol.getJsDocTags() : undefined }; } const { symbolKind, displayParts, documentation, tags } = typeChecker.runWithCancellationToken(cancellationToken, typeChecker => - SymbolDisplay.getSymbolDisplayPartsDocumentationAndSymbolKind(typeChecker, symbol, sourceFile, getContainerNode(node), node) + SymbolDisplay.getSymbolDisplayPartsDocumentationAndSymbolKind(typeChecker, symbol, sourceFile, getContainerNode(nodeForQuickInfo), nodeForQuickInfo) ); return { kind: symbolKind, kindModifiers: SymbolDisplay.getSymbolModifiers(symbol), - textSpan: createTextSpanFromNode(node, sourceFile), + textSpan: createTextSpanFromNode(nodeForQuickInfo, sourceFile), displayParts, documentation, tags, }; } + function getNodeForQuickInfo(node: Node): Node { + if (isNewExpression(node.parent) && node.pos === node.parent.pos) { + return node.parent.expression; + } + return node; + } + function shouldGetType(sourceFile: SourceFile, node: Node, position: number): boolean { switch (node.kind) { case SyntaxKind.Identifier: diff --git a/tests/baselines/reference/assigningFunctionToTupleIssuesError.errors.txt b/tests/baselines/reference/assigningFunctionToTupleIssuesError.errors.txt new file mode 100644 index 0000000000000..ca1af66c0873c --- /dev/null +++ b/tests/baselines/reference/assigningFunctionToTupleIssuesError.errors.txt @@ -0,0 +1,8 @@ +tests/cases/compiler/assigningFunctionToTupleIssuesError.ts(2,5): error TS2322: Type '() => void' is not assignable to type '[string]'. + + +==== tests/cases/compiler/assigningFunctionToTupleIssuesError.ts (1 errors) ==== + declare let a: () => void; + let b: [string] = a; + ~ +!!! error TS2322: Type '() => void' is not assignable to type '[string]'. \ No newline at end of file diff --git a/tests/baselines/reference/assigningFunctionToTupleIssuesError.js b/tests/baselines/reference/assigningFunctionToTupleIssuesError.js new file mode 100644 index 0000000000000..3de3da7b3cc41 --- /dev/null +++ b/tests/baselines/reference/assigningFunctionToTupleIssuesError.js @@ -0,0 +1,6 @@ +//// [assigningFunctionToTupleIssuesError.ts] +declare let a: () => void; +let b: [string] = a; + +//// [assigningFunctionToTupleIssuesError.js] +var b = a; diff --git a/tests/baselines/reference/assigningFunctionToTupleIssuesError.symbols b/tests/baselines/reference/assigningFunctionToTupleIssuesError.symbols new file mode 100644 index 0000000000000..7a499662aeffb --- /dev/null +++ b/tests/baselines/reference/assigningFunctionToTupleIssuesError.symbols @@ -0,0 +1,8 @@ +=== tests/cases/compiler/assigningFunctionToTupleIssuesError.ts === +declare let a: () => void; +>a : Symbol(a, Decl(assigningFunctionToTupleIssuesError.ts, 0, 11)) + +let b: [string] = a; +>b : Symbol(b, Decl(assigningFunctionToTupleIssuesError.ts, 1, 3)) +>a : Symbol(a, Decl(assigningFunctionToTupleIssuesError.ts, 0, 11)) + diff --git a/tests/baselines/reference/assigningFunctionToTupleIssuesError.types b/tests/baselines/reference/assigningFunctionToTupleIssuesError.types new file mode 100644 index 0000000000000..6c7d57f8483f2 --- /dev/null +++ b/tests/baselines/reference/assigningFunctionToTupleIssuesError.types @@ -0,0 +1,8 @@ +=== tests/cases/compiler/assigningFunctionToTupleIssuesError.ts === +declare let a: () => void; +>a : () => void + +let b: [string] = a; +>b : [string] +>a : () => void + diff --git a/tests/baselines/reference/conditionalTypes2.errors.txt b/tests/baselines/reference/conditionalTypes2.errors.txt index 1cf5b0eca44fa..4093bf46fb0ee 100644 --- a/tests/baselines/reference/conditionalTypes2.errors.txt +++ b/tests/baselines/reference/conditionalTypes2.errors.txt @@ -10,13 +10,13 @@ tests/cases/conformance/types/conditional/conditionalTypes2.ts(24,5): error TS23 Type 'keyof B' is not assignable to type 'keyof A'. Type 'string | number | symbol' is not assignable to type 'keyof A'. Type 'string' is not assignable to type 'keyof A'. - Type 'string' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf" | keyof A'. - Type 'keyof B' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf" | keyof A'. - Type 'string | number | symbol' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf" | keyof A'. - Type 'string' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf" | keyof A'. - Type 'keyof B' is not assignable to type 'keyof A'. - Type 'string | number | symbol' is not assignable to type 'keyof A'. - Type 'string' is not assignable to type 'keyof A'. + Type 'string' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'. + Type 'keyof B' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'. + Type 'string | number | symbol' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'. + Type 'string' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'. + Type 'keyof B' is not assignable to type '"valueOf"'. + Type 'string | number | symbol' is not assignable to type '"valueOf"'. + Type 'string' is not assignable to type '"valueOf"'. tests/cases/conformance/types/conditional/conditionalTypes2.ts(25,5): error TS2322: Type 'Invariant' is not assignable to type 'Invariant'. Types of property 'foo' are incompatible. Type 'A extends string ? keyof A : A' is not assignable to type 'B extends string ? keyof B : B'. @@ -73,13 +73,13 @@ tests/cases/conformance/types/conditional/conditionalTypes2.ts(75,12): error TS2 !!! error TS2322: Type 'keyof B' is not assignable to type 'keyof A'. !!! error TS2322: Type 'string | number | symbol' is not assignable to type 'keyof A'. !!! error TS2322: Type 'string' is not assignable to type 'keyof A'. -!!! error TS2322: Type 'string' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf" | keyof A'. -!!! error TS2322: Type 'keyof B' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf" | keyof A'. -!!! error TS2322: Type 'string | number | symbol' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf" | keyof A'. -!!! error TS2322: Type 'string' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf" | keyof A'. -!!! error TS2322: Type 'keyof B' is not assignable to type 'keyof A'. -!!! error TS2322: Type 'string | number | symbol' is not assignable to type 'keyof A'. -!!! error TS2322: Type 'string' is not assignable to type 'keyof A'. +!!! error TS2322: Type 'string' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'. +!!! error TS2322: Type 'keyof B' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'. +!!! error TS2322: Type 'string | number | symbol' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'. +!!! error TS2322: Type 'string' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'. +!!! error TS2322: Type 'keyof B' is not assignable to type '"valueOf"'. +!!! error TS2322: Type 'string | number | symbol' is not assignable to type '"valueOf"'. +!!! error TS2322: Type 'string' is not assignable to type '"valueOf"'. b = a; // Error ~ !!! error TS2322: Type 'Invariant' is not assignable to type 'Invariant'. diff --git a/tests/cases/compiler/assigningFunctionToTupleIssuesError.ts b/tests/cases/compiler/assigningFunctionToTupleIssuesError.ts new file mode 100644 index 0000000000000..befea50f81d94 --- /dev/null +++ b/tests/cases/compiler/assigningFunctionToTupleIssuesError.ts @@ -0,0 +1,2 @@ +declare let a: () => void; +let b: [string] = a; \ No newline at end of file diff --git a/tests/cases/fourslash/quickInfoOnNewKeyword01.ts b/tests/cases/fourslash/quickInfoOnNewKeyword01.ts new file mode 100644 index 0000000000000..f7d1e359b8d2a --- /dev/null +++ b/tests/cases/fourslash/quickInfoOnNewKeyword01.ts @@ -0,0 +1,18 @@ +/// + +////class Cat { +//// /** +//// * NOTE: this constructor is private! Please use the factory function +//// */ +//// private constructor() { } +//// +//// static makeCat() { new Cat(); } +////} +//// +////ne/*1*/w Ca/*2*/t(); + +verify.quickInfoAt('1', 'constructor Cat(): Cat', +'NOTE: this constructor is private! Please use the factory function'); + +verify.quickInfoAt('2', 'constructor Cat(): Cat', +'NOTE: this constructor is private! Please use the factory function');