Merge pull request #5736 from UBC-CPEN/issue-5173

Give more helpful error when trying to set default values on an interface.
This commit is contained in:
Daniel Rosenwasser 2015-12-01 13:05:09 -08:00
commit 4092de6993
10 changed files with 81 additions and 1 deletions

View file

@ -16285,11 +16285,17 @@ namespace ts {
if (checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_an_interface_must_directly_refer_to_a_built_in_symbol)) {
return true;
}
if (node.initializer) {
return grammarErrorOnNode(node.initializer, Diagnostics.An_interface_property_cannot_have_an_initializer);
}
}
else if (node.parent.kind === SyntaxKind.TypeLiteral) {
if (checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_a_type_literal_must_directly_refer_to_a_built_in_symbol)) {
return true;
}
if (node.initializer) {
return grammarErrorOnNode(node.initializer, Diagnostics.A_type_literal_property_cannot_have_an_initializer);
}
}
if (isInAmbientContext(node) && node.initializer) {

View file

@ -783,6 +783,14 @@
"category": "Error",
"code": 1245
},
"An interface property cannot have an initializer.": {
"category": "Error",
"code": 1246
},
"A type literal property cannot have an initializer.": {
"category": "Error",
"code": 1247
},
"'with' statements are not allowed in an async function block.": {
"category": "Error",
@ -2430,7 +2438,7 @@
"Not all code paths return a value.": {
"category": "Error",
"code": 7030
},
},
"You cannot rename this element.": {
"category": "Error",
"code": 8000

View file

@ -2259,6 +2259,14 @@ namespace ts {
property.name = name;
property.questionToken = questionToken;
property.type = parseTypeAnnotation();
if (token === SyntaxKind.EqualsToken) {
// Although type literal properties cannot not have initializers, we attempt
// to parse an initializer so we can report in the checker that an interface
// property or type literal property cannot have an initializer.
property.initializer = parseNonParameterInitializer();
}
parseTypeMemberSemicolon();
return finishNode(property);
}

View file

@ -587,6 +587,7 @@ namespace ts {
name: PropertyName; // Declared property name
questionToken?: Node; // Present on optional property
type?: TypeNode; // Optional type annotation
initializer?: Expression; // Optional initializer
}
// @kind(SyntaxKind.PropertyDeclaration)

View file

@ -0,0 +1,10 @@
tests/cases/compiler/errorOnInitializerInInterfaceProperty.ts(2,19): error TS1246: An interface property cannot have an initializer.
==== tests/cases/compiler/errorOnInitializerInInterfaceProperty.ts (1 errors) ====
interface Foo {
bar: number = 5;
~
!!! error TS1246: An interface property cannot have an initializer.
}

View file

@ -0,0 +1,7 @@
//// [errorOnInitializerInInterfaceProperty.ts]
interface Foo {
bar: number = 5;
}
//// [errorOnInitializerInInterfaceProperty.js]

View file

@ -0,0 +1,17 @@
tests/cases/compiler/errorOnInitializerInObjectTypeLiteralProperty.ts(2,19): error TS1247: A type literal property cannot have an initializer.
tests/cases/compiler/errorOnInitializerInObjectTypeLiteralProperty.ts(6,19): error TS1247: A type literal property cannot have an initializer.
==== tests/cases/compiler/errorOnInitializerInObjectTypeLiteralProperty.ts (2 errors) ====
var Foo: {
bar: number = 5;
~
!!! error TS1247: A type literal property cannot have an initializer.
};
let Bar: {
bar: number = 5;
~
!!! error TS1247: A type literal property cannot have an initializer.
};

View file

@ -0,0 +1,13 @@
//// [errorOnInitializerInObjectTypeLiteralProperty.ts]
var Foo: {
bar: number = 5;
};
let Bar: {
bar: number = 5;
};
//// [errorOnInitializerInObjectTypeLiteralProperty.js]
var Foo;
var Bar;

View file

@ -0,0 +1,3 @@
interface Foo {
bar: number = 5;
}

View file

@ -0,0 +1,7 @@
var Foo: {
bar: number = 5;
};
let Bar: {
bar: number = 5;
};