Skip to content

linter: Incorrect no-unassigned-vars lint with assignment assertion #14621

@tealsnow

Description

@tealsnow

The typescript syntax:

let some_var!: SomeType;

Exists to assert that this variable will be assigned before use.
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#definite-assignment-assertions

But the linter incorrectly warns about the assignment with no-unassigned-vars, claiming it is never assigned. Again, I stress, the whole point of the syntax is to assert to the typescript compiler that it will be assigned in a way that the compiler cannot figure out itself.

This is used in particular with solidjs for refs:

let ref!: HTMLDivElement;

onMount(() => {
    // can use ref without errors, since it will be assigned by the time it is used
})

return <div ref={ref}>
   ...
</div>

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions