镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Description

Reported by Yehuda Katz (@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 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 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

      Issue actions