Skip to content

Give more helpful error when trying to set default values on an interface. #5736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 1, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 10 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ namespace ts {
}

// Because of module/namespace merging, a module's exports are in scope,
// yet we never want to treat an export specifier as putting a member in scope.
// yet we never want to treat an export specifier as putting a member in scope.
// Therefore, if the name we find is purely an export specifier, it is not actually considered in scope.
// Two things to note about this:
// 1. We have to check this without calling getSymbol. The problem with calling getSymbol
Expand Down Expand Up @@ -11398,7 +11398,7 @@ namespace ts {
// we can get here in two cases
// 1. mixed static and instance class members
// 2. something with the same name was defined before the set of overloads that prevents them from merging
// here we'll report error only for the first case since for second we should already report error in binder
// here we'll report error only for the first case since for second we should already report error in binder
if (reportError) {
const diagnostic = node.flags & NodeFlags.Static ? Diagnostics.Function_overload_must_be_static : Diagnostics.Function_overload_must_not_be_static;
error(errorNode, diagnostic);
Expand Down Expand Up @@ -12065,8 +12065,8 @@ namespace ts {
const symbol = getSymbolOfNode(node);
const localSymbol = node.localSymbol || symbol;

// Since the javascript won't do semantic analysis like typescript,
// if the javascript file comes before the typescript file and both contain same name functions,
// Since the javascript won't do semantic analysis like typescript,
// if the javascript file comes before the typescript file and both contain same name functions,
// checkFunctionOrConstructorSymbol wouldn't be called if we didnt ignore javascript function.
const firstDeclaration = forEach(localSymbol.declarations,
// Get first non javascript function declaration
Expand Down Expand Up @@ -16227,11 +16227,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) {
Expand Down
10 changes: 9 additions & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -2418,7 +2426,7 @@
"Not all code paths return a value.": {
"category": "Error",
"code": 7030
},
},
"You cannot rename this element.": {
"category": "Error",
"code": 8000
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2247,6 +2247,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);
}
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//// [errorOnInitializerInInterfaceProperty.ts]
interface Foo {
bar: number = 5;
}


//// [errorOnInitializerInInterfaceProperty.js]
Original file line number Diff line number Diff line change
@@ -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.
};

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//// [errorOnInitializerInObjectTypeLiteralProperty.ts]
var Foo: {
bar: number = 5;
};

let Bar: {
bar: number = 5;
};


//// [errorOnInitializerInObjectTypeLiteralProperty.js]
var Foo;
var Bar;
3 changes: 3 additions & 0 deletions tests/cases/compiler/errorOnInitializerInInterfaceProperty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface Foo {
bar: number = 5;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var Foo: {
bar: number = 5;
};

let Bar: {
bar: number = 5;
};