Skip to content
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

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

Closed
lodo1995 opened this issue Mar 12, 2018 · 3 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@lodo1995
Copy link

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:

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Mar 13, 2018
@RyanCavanaugh
Copy link
Member

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

@lodo1995
Copy link
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.

@typescript-bot
Copy link
Collaborator

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

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants