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

Narrowed type of variable isn't obtained by aliases of the variable #26372

Closed
sandersn opened this issue Aug 10, 2018 · 3 comments
Closed

Narrowed type of variable isn't obtained by aliases of the variable #26372

sandersn opened this issue Aug 10, 2018 · 3 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@sandersn
Copy link
Member

sandersn commented Aug 10, 2018

function f() {}
f.p = { x: "" };
const p = f.p;
if (1 + 1 < 2) {
    f.p = { x: 0 };
}
f.p.x = 0;
p.x.toUpperCase(); // No compile error, crash at runtime

Expected behavior:

Error on p.x.toUpperCase()

Actual behavior:
No error.

@sandersn sandersn added the Bug A bug in TypeScript label Aug 10, 2018
@sandersn sandersn changed the title JS: Evolving type of expando property isn't obtained by destructuring references JS: Evolving type of expando property isn't obtained by references to the property Aug 10, 2018
@sandersn
Copy link
Member Author

Unfortunately, this bug repros with any variable:

var ex: { x: string } | { x: number } = { x: "" }
const p = ex
if (1 + 1 < 2) {
    ex = { x: 0 }
}
ex.x = 0
p.x.toUpperCase();

@sandersn sandersn changed the title JS: Evolving type of expando property isn't obtained by references to the property Narrowed type of variable isn't obtained by aliases of the variable Aug 15, 2018
@sandersn
Copy link
Member Author

@ahejlsberg I believe this bug exposes a design limitation of the control flow algorithm that we don't intend to fix. Can you confirm?

@RyanCavanaugh RyanCavanaugh added Design Limitation Constraints of the existing architecture prevent this from being fixed and removed Bug A bug in TypeScript labels Aug 24, 2018
@RyanCavanaugh
Copy link
Member

Aliasing effects are definitely missed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

2 participants