镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

strictPropertyInitialization does not work on instance fields with string names #22486

Description

In the code below, removing the quotes from the instance field name triggers the expected compile-time error. I thought that maybe quoted fields are not checked, but apparently strictNullChecks works on quoted fields, so i think strictPropertyInitialization should work too.

TypeScript Version: 2.7.2

Search Terms: strictPropertyInitialization

Code

class Example
{
    public "field": string;
}

console.log(new Example()["field"].toLowerCase());

Expected behavior: error: Property 'field' has no initializer and is not definitely assigned in the constructor.

Actual behavior: compiles fine, crashes at runtime due to undefined field

Playground Link: https://www.typescriptlang.org/play/#src=class%20Example%0A%7B%0A%20%20%20%20public%20%22example%3Afield%22%3A%20string%3B%0A%7D%0A%0Aconsole.log(new%20Example()%5B%22example%3Afield%22%5D.toLowerCase())%3B

Related Issues:

Activity

  1. RyanCavanaugh commented on Mar 13, 2018

    @RyanCavanaugh
    Member

    Quoted identifiers are not subject to this constraint; see the comment in #20075

  2. lodo1995 commented on Mar 13, 2018

    @lodo1995
    Author

    Ok, now I see it was intended. But I don't see why it was decided to do it this way. It's easy for this kind of bug to sneak into the code, as setting strictPropertyInitialization makes the developer feel he is safe, when in fact he isn't.

  3. typescript-bot commented on Mar 28, 2018

    @typescript-bot
    Contributor

    Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

  4. locked and limited conversation to collaborators on Jul 25, 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

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions