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

Why TDZ checks are not implemented #54343

Closed
yf-yang opened this issue May 22, 2023 · 1 comment
Closed

Why TDZ checks are not implemented #54343

yf-yang opened this issue May 22, 2023 · 1 comment
Labels
Question An issue which isn't directly actionable in code

Comments

@yf-yang
Copy link

yf-yang commented May 22, 2023

Bug Report

It may not be a bug, because I see that you clearly know what TDZ check is (such as the code example in this issue), but it is not applied. I did find some related issues reported by TypeScript team members such as #5174 several years before, but it still passes the check now.

Just out of curiosity, would you please explain why such checks are not (fully) implemented?

🔎 Search Terms

TDZ check

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries.

⏯ Playground Link

Playground link with relevant code

💻 Code

// https://github.com/microsoft/TypeScript/issues/5174
function f(x: number) {
    switch(x) {
        case 1:
            let x = 1;
        case 2:
            x = 2; // should be error because of TDZ
    }
}

// https://github.com/microsoft/TypeScript/issues/52924#issue-1595962667
function h() {
    let result = g();
    let x: number = 10;
    return result;

    function g() {
        return x;
    }
}

🙁 Actual behavior

No error reported.

🙂 Expected behavior

TDZ check should be applied.

@RyanCavanaugh
Copy link
Member

TL;DR it's quite difficult to detect these with high enough precision to make it worthwhile.

TS does have TDZ checking for cases where it can easily be detected that a TDZ violation will always occur. for cases where a TDZ violation may or may not occur, it's more tricky since this is a sort of error that is not silenceable through the type system, so we don't want to issue it unless we can be very sure that a violation is definitely occurring. And it's easy to make programs which appear like they might have a TDZ violation, but don't by construction. and, after all, if your function body really does unconditionally raise a TDC error, then finding that out at build time vs at first execution has fairly low value compared to other checks we might be making.

For switch blocks, see this PR: #47160. This seems to be a pretty rare case it doesn't come up very often and it's actually pretty difficult to only issue correct errors in this case, so it didn't seem like a good use of finite time

For function analysis, see also #11498

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label May 22, 2023
@yf-yang yf-yang closed this as completed May 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

2 participants