Skip to content

Lint rule for definite assignment assertions with immediate initialization #21207

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

Closed
DanielRosenwasser opened this issue Jan 16, 2018 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@DanielRosenwasser
Copy link
Member

var x!: number = 2;

class Class {
  a!: number = 10
}

Seems like a weird construct. In #20117 we said we should write a lint rule for this.

@DanielRosenwasser DanielRosenwasser added Infrastructure Issue relates to TypeScript team infrastructure Community Tooling This might be better handled by tooling from the community instead of built into TypeScript labels Jan 16, 2018
@ajafff
Copy link
Contributor

ajafff commented Jan 17, 2018

@DanielRosenwasser I just tried typescript@next and I get an error from typescript for both definite assignment assertions.

A definite assignment assertion '!' is not permitted in this context.

That means there is no need for a lint rule for declarations with initializer.


I'm currently only linting for assertions that are unnecessary because the type is an undefined union:

let foo!: string | undefined; // unnecessary assertion

class Foo {
    prop!: string; // unnecessary assertion
    constructor() {
        this.prop = 1;
    }
}

To detect if a definite assignment assertion is unnecessary because the property is initialized in the constructor (see above), we'd need a new API (similar to #20221 but specialized for properties)

@mhegazy
Copy link
Contributor

mhegazy commented Jan 17, 2018

@ajafff u are right about the new API. I think this can be lumped with #20221, asking if the property is definitely assigned at the end of the constructor.

@mhegazy mhegazy added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Community Tooling This might be better handled by tooling from the community instead of built into TypeScript Infrastructure Issue relates to TypeScript team infrastructure labels Jan 17, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Jan 17, 2018

Marking as Working as Intended since the examples in the OP are already reported as errors.

@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.

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

4 participants