diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 78088de0c3d..31d5f2da97f 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1028,7 +1028,7 @@ namespace ts { const potentialReflectCollisions: Node[] = []; const potentialAlwaysCheckedUnusedTypes: { node: Node; - name: BindingName; + name: Identifier; diagnostic: DiagnosticMessage; }[] = []; const awaitedTypeStack: number[] = []; @@ -37579,6 +37579,14 @@ namespace ts { }); } + function checkPotentialAlwaysCheckedUnusedTypes() { + for (const { node, name, diagnostic } of potentialAlwaysCheckedUnusedTypes) { + if (!(getSymbolOfNode(node)?.isReferenced! & SymbolFlags.Type)) { + error(name, diagnostic, declarationNameToString(name)); + } + } + } + function bindingNameText(name: BindingName): string { switch (name.kind) { case SyntaxKind.Identifier: @@ -37933,7 +37941,7 @@ namespace ts { potentialAlwaysCheckedUnusedTypes.push({ node, name: node.name, - diagnostic: Diagnostics.Renaming_a_property_in_destructuring_assignment_is_only_allowed_in_a_function_or_constructor_implementation + diagnostic: Diagnostics.Variable_0_is_not_used_Did_you_mean_to_write_a_type_annotation_for_whole_object }); return; } @@ -41801,12 +41809,8 @@ namespace ts { } }); } - if (!node.isDeclarationFile && potentialAlwaysCheckedUnusedTypes.length) { - for (const { node, name, diagnostic } of potentialAlwaysCheckedUnusedTypes) { - if (!(getSymbolOfNode(node)?.isReferenced! & SymbolFlags.Type)) { - error(name, diagnostic); - } - } + if (!node.isDeclarationFile) { + checkPotentialAlwaysCheckedUnusedTypes(); } }); diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 6d1df373a29..1df0c5f12a2 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3487,7 +3487,7 @@ "category": "Error", "code": 2841 }, - "Renaming a property in destructuring assignment is only allowed in a function or constructor implementation.": { + "Variable '{0}' is not used. Did you mean to write a type annotation for whole object?": { "category": "Error", "code": 2842 }, diff --git a/tests/baselines/reference/renamingDestructuredPropertyInFunctionType.errors.txt b/tests/baselines/reference/renamingDestructuredPropertyInFunctionType.errors.txt index 5e6ce635421..dbef15bf69d 100644 --- a/tests/baselines/reference/renamingDestructuredPropertyInFunctionType.errors.txt +++ b/tests/baselines/reference/renamingDestructuredPropertyInFunctionType.errors.txt @@ -1,16 +1,16 @@ -tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(3,17): error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. -tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(4,17): error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. -tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(5,17): error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. -tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(6,17): error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. -tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(8,17): error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. -tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(13,17): error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. -tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(14,17): error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. -tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(15,17): error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. -tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(16,17): error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. -tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(18,17): error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. -tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(24,16): error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. -tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(27,9): error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. -tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(30,13): error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. +tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(3,17): error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? +tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(4,17): error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? +tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(5,17): error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? +tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(6,17): error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? +tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(8,17): error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? +tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(13,17): error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? +tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(14,17): error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? +tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(15,17): error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? +tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(16,17): error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? +tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(18,17): error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? +tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(24,16): error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? +tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(27,9): error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? +tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(30,13): error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? ==== tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts (13 errors) ==== @@ -18,40 +18,40 @@ tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(30,13): error type F1 = (arg: number) => any; // OK type F2 = ({ a: string }: O) => any; // Error ~~~~~~ -!!! error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. +!!! error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? type F3 = ({ a: string, b, c }: O) => any; // Error ~~~~~~ -!!! error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. +!!! error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? type F4 = ({ a: string }: O) => any; // Error ~~~~~~ -!!! error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. +!!! error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? type F5 = ({ a: string, b, c }: O) => any; // Error ~~~~~~ -!!! error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. +!!! error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? type F6 = ({ a: string }) => typeof string; // OK type F7 = ({ a: string, b: number }) => typeof number; // Error ~~~~~~ -!!! error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. +!!! error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? type F8 = ({ a, b: number }) => typeof number; // OK type F9 = ([a, b, c]) => void; // Error type G1 = (arg: number) => any; // OK type G2 = ({ a: string }: O) => any; // Error ~~~~~~ -!!! error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. +!!! error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? type G3 = ({ a: string, b, c }: O) => any; // Error ~~~~~~ -!!! error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. +!!! error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? type G4 = ({ a: string }: O) => any; // Error ~~~~~~ -!!! error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. +!!! error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? type G5 = ({ a: string, b, c }: O) => any; // Error ~~~~~~ -!!! error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. +!!! error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? type G6 = ({ a: string }) => typeof string; // OK type G7 = ({ a: string, b: number }) => typeof number; // Error ~~~~~~ -!!! error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. +!!! error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? type G8 = ({ a, b: number }) => typeof number; // OK type G9 = ([a, b, c]) => void; // Error @@ -59,15 +59,15 @@ tests/cases/compiler/renamingDestructuredPropertyInFunctionType.ts(30,13): error method1(arg: number): any; // OK method2({ a: string }): any; // Error ~~~~~~ -!!! error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. +!!! error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? (arg: number): any; // OK ({ a: string }): any; // Error ~~~~~~ -!!! error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. +!!! error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? new (arg: number): any; // OK new ({ a: string }): any; // Error ~~~~~~ -!!! error TS2842: Renaming a property in destructuring assignment is only allowed in a function or constructor implementation. +!!! error TS2842: Variable 'string' is not used. Did you mean to write a type annotation for whole object? } \ No newline at end of file