Skip to content

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

Closed

Description

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?)

Activity

  1. added this to the milestone on Oct 8, 2015
  2. delta-nry commented on Oct 17, 2015

    @delta-nry
    Contributor

    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?

  3. DanielRosenwasser commented on Oct 18, 2015

    @DanielRosenwasser
    Member

    Nope, it's just good that you asked so others know you're working on it. Go for it. 😄

  4. CyrusNajmabadi commented on Oct 20, 2015

    @CyrusNajmabadi
    Contributor

    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.

  5. jbondc commented on Oct 23, 2015

    @jbondc
    Contributor

    Oops, moving my comments to #4878

  6. delta-nry commented on Oct 26, 2015

    @delta-nry
    Contributor

    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?

  7. DanielRosenwasser commented on Dec 1, 2015

    @DanielRosenwasser
    Member
  8. modified the milestones: TypeScript 1.8, on Dec 1, 2015
  9. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptEffort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".FixedA PR has been merged for this issueHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions