Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -7014,6 +7014,10 @@
"category": "Error",
"code": 9037
},
"Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.": {
"category": "Error",
"code": 9038
},
"JSX attributes must only be assigned a non-empty 'expression'.": {
"category": "Error",
"code": 17000
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/expressionToTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ export function createSyntacticTypeNodeBuilder(options: CompilerOptions, resolve
}
else if (prop.name.kind === SyntaxKind.ComputedPropertyName) {
const expression = prop.name.expression;
if (!isPrimitiveLiteralValue(expression, /*includeBigInt*/ false) && !isEntityNameExpression(expression)) {
if (!isPrimitiveLiteralValue(expression, /*includeBigInt*/ false)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also code in typeFromObjectLiteral that is not effectively unused. isNonNarrowedBindableName also probably becomes useless since we can now syntactically determine if the computed property is valid so it can probably be removed from the resolver.

context.tracker.reportInferenceFallback(prop.name);
result = false;
}
Expand Down
19 changes: 12 additions & 7 deletions src/compiler/transformers/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ import {
isTupleTypeNode,
isTypeAliasDeclaration,
isTypeElement,
isTypeLiteralNode,
isTypeNode,
isTypeParameterDeclaration,
isTypeQueryNode,
Expand Down Expand Up @@ -995,16 +996,20 @@ export function transformDeclarations(context: TransformationContext) {
if (shouldStripInternal(input)) return;
if (isDeclaration(input)) {
if (isDeclarationAndNotVisible(input)) return;
if (hasDynamicName(input) && !resolver.isLateBound(getParseTreeNode(input) as Declaration)) {
if (hasDynamicName(input)) {
if (
isolatedDeclarations
// Classes usually elide properties with computed names that are not of a literal type
// Classes and object literals usually elide properties with computed names that are not of a literal type
// In isolated declarations TSC needs to error on these as we don't know the type in a DTE.
&& isClassDeclaration(input.parent)
&& isEntityNameExpression(input.name.expression)
// If the symbol is not accessible we get another TS error no need to add to that
&& resolver.isEntityNameVisible(input.name.expression, input.parent).accessibility === SymbolAccessibility.Accessible
&& !resolver.isNonNarrowedBindableName(input.name)
&& (isClassDeclaration(input.parent) || isObjectLiteralExpression(input.parent))
) {
context.addDiagnostic(createDiagnosticForNode(input, Diagnostics.Computed_property_names_on_class_or_object_literals_cannot_be_inferred_with_isolatedDeclarations));
}
if (
isolatedDeclarations
// Type declarations just need to double-check that the input computed name is an entity name expression
&& (isInterfaceDeclaration(input.parent) || isTypeLiteralNode(input.parent))
&& !isEntityNameExpression(input.name.expression)
) {
context.addDiagnostic(createDiagnosticForNode(input, Diagnostics.Computed_properties_must_be_number_or_string_literals_variables_or_dotted_expressions_with_isolatedDeclarations));
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/transformers/declarations/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ export function createGetIsolatedDeclarationErrors(resolver: EmitResolver) {
[SyntaxKind.VariableDeclaration]: Diagnostics.Variable_must_have_an_explicit_type_annotation_with_isolatedDeclarations,
[SyntaxKind.PropertyDeclaration]: Diagnostics.Property_must_have_an_explicit_type_annotation_with_isolatedDeclarations,
[SyntaxKind.PropertySignature]: Diagnostics.Property_must_have_an_explicit_type_annotation_with_isolatedDeclarations,
[SyntaxKind.ComputedPropertyName]: Diagnostics.Computed_properties_must_be_number_or_string_literals_variables_or_dotted_expressions_with_isolatedDeclarations,
[SyntaxKind.ComputedPropertyName]: Diagnostics.Computed_property_names_on_class_or_object_literals_cannot_be_inferred_with_isolatedDeclarations,
[SyntaxKind.SpreadAssignment]: Diagnostics.Objects_that_contain_spread_assignments_can_t_be_inferred_with_isolatedDeclarations,
[SyntaxKind.ShorthandPropertyAssignment]: Diagnostics.Objects_that_contain_shorthand_properties_can_t_be_inferred_with_isolatedDeclarations,
[SyntaxKind.ArrayLiteralExpression]: Diagnostics.Only_const_arrays_can_be_inferred_with_isolatedDeclarations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ const errorCodes = [
Diagnostics.Property_must_have_an_explicit_type_annotation_with_isolatedDeclarations.code,
Diagnostics.Expression_type_can_t_be_inferred_with_isolatedDeclarations.code,
Diagnostics.Binding_elements_can_t_be_exported_directly_with_isolatedDeclarations.code,
Diagnostics.Computed_property_names_on_class_or_object_literals_cannot_be_inferred_with_isolatedDeclarations.code,
Diagnostics.Computed_properties_must_be_number_or_string_literals_variables_or_dotted_expressions_with_isolatedDeclarations.code,
Diagnostics.Enum_member_initializers_must_be_computable_without_references_to_external_symbols_with_isolatedDeclarations.code,
Diagnostics.Extends_clause_can_t_contain_an_expression_with_isolatedDeclarations.code,
Expand Down
38 changes: 25 additions & 13 deletions tests/baselines/reference/computedPropertiesNarrowed.errors.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
computedPropertiesNarrowed.ts(5,5): error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
computedPropertiesNarrowed.ts(18,20): error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
computedPropertiesNarrowed.ts(22,5): error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
computedPropertiesNarrowed.ts(26,5): error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
computedPropertiesNarrowed.ts(37,5): error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
computedPropertiesNarrowed.ts(47,5): error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
computedPropertiesNarrowed.ts(5,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
computedPropertiesNarrowed.ts(11,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
computedPropertiesNarrowed.ts(18,20): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
computedPropertiesNarrowed.ts(22,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
computedPropertiesNarrowed.ts(26,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
computedPropertiesNarrowed.ts(31,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
computedPropertiesNarrowed.ts(37,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
computedPropertiesNarrowed.ts(42,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
computedPropertiesNarrowed.ts(47,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.


==== computedPropertiesNarrowed.ts (6 errors) ====
==== computedPropertiesNarrowed.ts (9 errors) ====
const x: 0 | 1 = Math.random()? 0: 1;
declare function assert(n: number): asserts n is 1;
assert(x);
export let o = {
[x]: 1 // error narrow type !== declared type
~~~
!!! error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
!!! related TS9027 computedPropertiesNarrowed.ts:4:12: Add a type annotation to the variable o.
}


const y: 0 = 0
export let o2 = {
[y]: 1 // ok literal computed type
~~~
!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
!!! related TS9027 computedPropertiesNarrowed.ts:10:12: Add a type annotation to the variable o2.
}

// literals are ok
Expand All @@ -29,48 +35,54 @@ computedPropertiesNarrowed.ts(47,5): error TS9014: Computed properties must be n

export let o32 = { [1-1]: 1 } // error number
~~~~~
!!! error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
!!! related TS9027 computedPropertiesNarrowed.ts:18:12: Add a type annotation to the variable o32.

let u = Symbol();
export let o4 = {
[u]: 1 // Should error, nut a unique symbol
~~~
!!! error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
!!! related TS9027 computedPropertiesNarrowed.ts:21:12: Add a type annotation to the variable o4.
}

export let o5 ={
[Symbol()]: 1 // Should error
~~~~~~~~~~
!!! error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
!!! related TS9027 computedPropertiesNarrowed.ts:25:12: Add a type annotation to the variable o5.
}

const uu: unique symbol = Symbol();
export let o6 = {
[uu]: 1 // Should be ok
~~~~
!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
!!! related TS9027 computedPropertiesNarrowed.ts:30:12: Add a type annotation to the variable o6.
}


function foo (): 1 { return 1; }
export let o7 = {
[foo()]: 1 // Should error
~~~~~~~
!!! error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
!!! related TS9027 computedPropertiesNarrowed.ts:36:12: Add a type annotation to the variable o7.
};

let E = { A: 1 } as const
export const o8 = {
[E.A]: 1 // Fresh
~~~~~
!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
!!! related TS9027 computedPropertiesNarrowed.ts:41:14: Add a type annotation to the variable o8.
}

function ns() { return { v: 0 } as const }
export const o9 = {
[ns().v]: 1
~~~~~~~~
!!! error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
!!! related TS9027 computedPropertiesNarrowed.ts:46:14: Add a type annotation to the variable o9.
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,27 @@ isolatedDeclarationErrorsClasses.ts(11,9): error TS9009: At least one accessor m
isolatedDeclarationErrorsClasses.ts(12,9): error TS7032: Property 'setOnly' implicitly has type 'any', because its set accessor lacks a parameter type annotation.
isolatedDeclarationErrorsClasses.ts(12,17): error TS7006: Parameter 'value' implicitly has an 'any' type.
isolatedDeclarationErrorsClasses.ts(36,5): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
isolatedDeclarationErrorsClasses.ts(36,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(36,6): error TS2304: Cannot find name 'missing'.
isolatedDeclarationErrorsClasses.ts(38,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(40,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(42,5): error TS9008: Method must have an explicit return type annotation with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(42,5): error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(44,5): error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(42,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(44,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(44,35): error TS7006: Parameter 'v' implicitly has an 'any' type.
isolatedDeclarationErrorsClasses.ts(44,35): error TS9011: Parameter must have an explicit type annotation with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(46,9): error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(46,9): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(48,9): error TS7032: Property '[noParamAnnotationStringName]' implicitly has type 'any', because its set accessor lacks a parameter type annotation.
isolatedDeclarationErrorsClasses.ts(48,9): error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(48,9): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(48,39): error TS7006: Parameter 'value' implicitly has an 'any' type.
isolatedDeclarationErrorsClasses.ts(50,5): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
isolatedDeclarationErrorsClasses.ts(50,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(55,5): error TS1169: A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type.
isolatedDeclarationErrorsClasses.ts(56,5): error TS7010: '[noAnnotationLiteralName]', which lacks return-type annotation, implicitly has an 'any' return type.
isolatedDeclarationErrorsClasses.ts(56,5): error TS9013: Expression type can't be inferred with --isolatedDeclarations.


==== isolatedDeclarationErrorsClasses.ts (23 errors) ====
==== isolatedDeclarationErrorsClasses.ts (27 errors) ====
export class Cls {

field = 1 + 1;
Expand Down Expand Up @@ -83,23 +87,29 @@ isolatedDeclarationErrorsClasses.ts(56,5): error TS9013: Expression type can't b
[missing] = 1;
~~~~~~~~~
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
~~~~~~~~~
!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
~~~~~~~
!!! error TS2304: Cannot find name 'missing'.

[noAnnotationLiteralName](): void { }
~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.

[noParamAnnotationLiteralName](v: string): void { }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.

[noAnnotationStringName]() { }
~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS9008: Method must have an explicit return type annotation with --isolatedDeclarations.
!!! related TS9034 isolatedDeclarationErrorsClasses.ts:42:5: Add a return type to the method
~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.

[noParamAnnotationStringName](v): void { }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
~
!!! error TS7006: Parameter 'v' implicitly has an 'any' type.
~
Expand All @@ -108,19 +118,21 @@ isolatedDeclarationErrorsClasses.ts(56,5): error TS9013: Expression type can't b

get [noAnnotationStringName]() { return 0;}
~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.

set [noParamAnnotationStringName](value) { }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS7032: Property '[noParamAnnotationStringName]' implicitly has type 'any', because its set accessor lacks a parameter type annotation.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
~~~~~
!!! error TS7006: Parameter 'value' implicitly has an 'any' type.

[("A" + "B") as "AB"] = 1;
~~~~~~~~~~~~~~~~~~~~~
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
~~~~~~~~~~~~~~~~~~~~~
!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.

}

Expand Down
Loading