Skip to content

tsc --strict is allowing me to use an assigned var. #31198

Closed
@StokeMasterJack

Description

@StokeMasterJack

TypeScript Version: 3.4.5

Search Terms:

Code

let aaa: string | null;
function getAaa(): string {
    if (aaa === null) {  aaa = 'hello'; }
    return aaa;
}
console.log(getAaa().length);

Expected behavior:
I expected a compile error (when using --strict). Something like this:
error TS2454: Variable 'aaa' is used before being assigned.

Actual behavior:
No compile error. But got this runtime error:
Cannot read property 'length' of undefined

Playground Link: https://www.typescriptlang.org/play/#src=let%20aaa%3A%20string%20%7C%20null%3B%0D%0A%0D%0Afunction%20getAaa()%3A%20string%20%7B%0D%0A%20%20%20%20if%20(aaa%20%3D%3D%3D%20null)%20%7B%0D%0A%20%20%20%20%20%20%20%20aaa%20%3D%20'hello'%3B%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20return%20aaa%3B%0D%0A%7D%0D%0A%20%0D%0A%0D%0Aconsole.log(getAaa().length)%3B

Activity

nmain

nmain commented on May 2, 2019

@nmain

See #28013

typescript-bot

typescript-bot commented on May 11, 2019

@typescript-bot
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

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

        @StokeMasterJack@weswigham@typescript-bot@nmain

        Issue actions

          tsc --strict is allowing me to use an assigned var. · Issue #31198 · microsoft/TypeScript