Skip to content

No easy way to convince TypeScript a variable has been initialized. #12855

Closed
@DanielRosenwasser

Description

@DanielRosenwasser

Reported by @wycats. Compile this under strictNullChecks and noImplicitAny.

declare function someAbstraction(x: any): any;

let x: string;

someAbstraction(() => {
  x = "hello";
});

// All of the following are errors.
x;
x as any;
x!;

I'd argue that both type assertions should be able to signal to the type system that the variable has been initialized.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions