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

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

Closed
DanielRosenwasser opened this issue Dec 12, 2016 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@DanielRosenwasser
Copy link
Member

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.

@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label Dec 12, 2016
@RyanCavanaugh
Copy link
Member

Duplicate #11463

@mhegazy mhegazy added Duplicate An existing issue was already created and removed Bug A bug in TypeScript labels Dec 12, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Dec 13, 2016

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

@mhegazy mhegazy closed this as completed Dec 13, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants