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

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

Closed
StokeMasterJack opened this issue May 1, 2019 · 2 comments
Closed

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

StokeMasterJack opened this issue May 1, 2019 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@StokeMasterJack
Copy link

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

@nmain
Copy link

nmain commented May 2, 2019

See #28013

@weswigham weswigham added the Duplicate An existing issue was already created label May 8, 2019
@typescript-bot
Copy link
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
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants