Skip to content

Commit 4092de6

Browse files
Merge pull request #5736 from UBC-CPEN/issue-5173
Give more helpful error when trying to set default values on an interface.
2 parents 5e86ca1 + 144d24c commit 4092de6

10 files changed

+81
-1
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16285,11 +16285,17 @@ namespace ts {
1628516285
if (checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_an_interface_must_directly_refer_to_a_built_in_symbol)) {
1628616286
return true;
1628716287
}
16288+
if (node.initializer) {
16289+
return grammarErrorOnNode(node.initializer, Diagnostics.An_interface_property_cannot_have_an_initializer);
16290+
}
1628816291
}
1628916292
else if (node.parent.kind === SyntaxKind.TypeLiteral) {
1629016293
if (checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_a_type_literal_must_directly_refer_to_a_built_in_symbol)) {
1629116294
return true;
1629216295
}
16296+
if (node.initializer) {
16297+
return grammarErrorOnNode(node.initializer, Diagnostics.A_type_literal_property_cannot_have_an_initializer);
16298+
}
1629316299
}
1629416300

1629516301
if (isInAmbientContext(node) && node.initializer) {

src/compiler/diagnosticMessages.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,14 @@
783783
"category": "Error",
784784
"code": 1245
785785
},
786+
"An interface property cannot have an initializer.": {
787+
"category": "Error",
788+
"code": 1246
789+
},
790+
"A type literal property cannot have an initializer.": {
791+
"category": "Error",
792+
"code": 1247
793+
},
786794

787795
"'with' statements are not allowed in an async function block.": {
788796
"category": "Error",
@@ -2430,7 +2438,7 @@
24302438
"Not all code paths return a value.": {
24312439
"category": "Error",
24322440
"code": 7030
2433-
},
2441+
},
24342442
"You cannot rename this element.": {
24352443
"category": "Error",
24362444
"code": 8000

src/compiler/parser.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,6 +2259,14 @@ namespace ts {
22592259
property.name = name;
22602260
property.questionToken = questionToken;
22612261
property.type = parseTypeAnnotation();
2262+
2263+
if (token === SyntaxKind.EqualsToken) {
2264+
// Although type literal properties cannot not have initializers, we attempt
2265+
// to parse an initializer so we can report in the checker that an interface
2266+
// property or type literal property cannot have an initializer.
2267+
property.initializer = parseNonParameterInitializer();
2268+
}
2269+
22622270
parseTypeMemberSemicolon();
22632271
return finishNode(property);
22642272
}

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ namespace ts {
587587
name: PropertyName; // Declared property name
588588
questionToken?: Node; // Present on optional property
589589
type?: TypeNode; // Optional type annotation
590+
initializer?: Expression; // Optional initializer
590591
}
591592

592593
// @kind(SyntaxKind.PropertyDeclaration)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/compiler/errorOnInitializerInInterfaceProperty.ts(2,19): error TS1246: An interface property cannot have an initializer.
2+
3+
4+
==== tests/cases/compiler/errorOnInitializerInInterfaceProperty.ts (1 errors) ====
5+
interface Foo {
6+
bar: number = 5;
7+
~
8+
!!! error TS1246: An interface property cannot have an initializer.
9+
}
10+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [errorOnInitializerInInterfaceProperty.ts]
2+
interface Foo {
3+
bar: number = 5;
4+
}
5+
6+
7+
//// [errorOnInitializerInInterfaceProperty.js]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests/cases/compiler/errorOnInitializerInObjectTypeLiteralProperty.ts(2,19): error TS1247: A type literal property cannot have an initializer.
2+
tests/cases/compiler/errorOnInitializerInObjectTypeLiteralProperty.ts(6,19): error TS1247: A type literal property cannot have an initializer.
3+
4+
5+
==== tests/cases/compiler/errorOnInitializerInObjectTypeLiteralProperty.ts (2 errors) ====
6+
var Foo: {
7+
bar: number = 5;
8+
~
9+
!!! error TS1247: A type literal property cannot have an initializer.
10+
};
11+
12+
let Bar: {
13+
bar: number = 5;
14+
~
15+
!!! error TS1247: A type literal property cannot have an initializer.
16+
};
17+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [errorOnInitializerInObjectTypeLiteralProperty.ts]
2+
var Foo: {
3+
bar: number = 5;
4+
};
5+
6+
let Bar: {
7+
bar: number = 5;
8+
};
9+
10+
11+
//// [errorOnInitializerInObjectTypeLiteralProperty.js]
12+
var Foo;
13+
var Bar;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface Foo {
2+
bar: number = 5;
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var Foo: {
2+
bar: number = 5;
3+
};
4+
5+
let Bar: {
6+
bar: number = 5;
7+
};

0 commit comments

Comments
 (0)