You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Give more helpful error when trying to set default values on an interface #5173
Today I spent a moment scratching my head at the following snippet:
interface Foo {
bar: number = 5;
}
"Of course!" I say now, I was trying to set a default value on an interface, which makes no sense. TypeScript didn't see that this might be something I would do, and instead gave me a syntax error:
Error TS1005: ';' expected.
Error TS1131: Property or signature expected.
Error TS1128: Declaration or statement expected.
Would it be possible to get a more useful error message for this case (and potentially cases like this?)
I am part of a group of students from the University of British Columbia looking to contribute to a bug fix. Is there anything we must do to get assigned onto this bug?
The correct way to handle this would likely be to just allow parsing a full on property inside an interface. We can then report an error later on that an interface property cannot have an initializer. That way the parse tree is well formed, and features like formatting/navbars/etc. all work property.
I've been looking at the parser.ts file. It appears that in parseTypeMember(), if we return parsePropertyOrMethodDeclaration() for string and numeric literals instead of returning parsePropertyOrMethodSignature(), this will allow parsing full properties inside an interface. In this case, parsePropertyOrMethodDeclaration() should return parsePropertyDeclaration(). What is the best way for parsePropertyDeclaration() to report the desired error if an initializer is present on an interface property?
Activity
delta-nry commentedon Oct 17, 2015
I am part of a group of students from the University of British Columbia looking to contribute to a bug fix. Is there anything we must do to get assigned onto this bug?
DanielRosenwasser commentedon Oct 18, 2015
Nope, it's just good that you asked so others know you're working on it. Go for it. 😄
CyrusNajmabadi commentedon Oct 20, 2015
The correct way to handle this would likely be to just allow parsing a full on property inside an interface. We can then report an error later on that an interface property cannot have an initializer. That way the parse tree is well formed, and features like formatting/navbars/etc. all work property.
jbondc commentedon Oct 23, 2015
Oops, moving my comments to #4878
delta-nry commentedon Oct 26, 2015
I've been looking at the parser.ts file. It appears that in parseTypeMember(), if we return parsePropertyOrMethodDeclaration() for string and numeric literals instead of returning parsePropertyOrMethodSignature(), this will allow parsing full properties inside an interface. In this case, parsePropertyOrMethodDeclaration() should return parsePropertyDeclaration(). What is the best way for parsePropertyDeclaration() to report the desired error if an initializer is present on an interface property?
Implement microsoft#5173
Add tests for microsoft#5173
DanielRosenwasser commentedon Dec 1, 2015
Thanks Nathan Yee (@delta-nry)!