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.

Activity

RyanCavanaugh

RyanCavanaugh commented on Dec 12, 2016

@RyanCavanaugh
Member

Duplicate #11463

added
DuplicateAn existing issue was already created
and removed
BugA bug in TypeScript
on Dec 12, 2016
mhegazy

mhegazy commented on Dec 13, 2016

@mhegazy
Contributor

declaring x as string | undefined should make these all work though.

locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @DanielRosenwasser@RyanCavanaugh@mhegazy

        Issue actions

          No easy way to convince TypeScript a variable has been initialized. · Issue #12855 · microsoft/TypeScript