Skip to content

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

Closed
@lodo1995

Description

@lodo1995

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:

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