Skip to content

Detect "partial aliased conditions" where string typeof is stored in a const, instead of boolean typeof === expression #49583

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
4 of 5 tasks
peterflynn opened this issue Jun 16, 2022 · 1 comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@peterflynn
Copy link

Suggestion

πŸ” Search Terms

aliased conditional expressions, aliased condition, const typeof value, control flow analysis, type narrowing, type guards

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

TS 4.4 added "Control Flow Analysis of Aliased Conditions", which detects boolean consts that when used in conditionals should narrow the type. The suggestion here is to also detect & narrow the type when other consts (the string value of typeof) are incorporated inside boolean expressions (===) in a conditional.

πŸ“ƒ Motivating Example

(Playground)

// Works - 'arg' is narrowed to type 'string'
function example2_aliasedCheck(arg: unknown) {
    const isString = typeof arg === "string";
    if (isString) {
        console.log(arg.toUpperCase());
    }
}

// ERROR - 'arg' remains type 'unknown'
function example3_aliasedType(arg: unknown) {
    const t = typeof arg;
    if (t === "string") {
        console.log(arg.toUpperCase());  // <-- ERROR
    }
}

Interestingly, it seems like this pattern is already supported for discriminated type unions – in the TS 4.4 release notes above, see the example that reads "Analysis on discriminants in 4.4 also goes a little bit deeper."

This suggestion might potentially apply to other kinds of value-driven type narrowing as well, e.g. truthy checks that act as non-nullable type guards – though that appears to not work yet even with directly inlined checks, so it probably shouldn't be in scope for this suggestion.

πŸ’» Use Cases

The specific example where I saw this was code that 'cached' the typeof result in a const because it had a chain of ifs checking for different type values.

@MartinJohns
Copy link
Contributor

though that appears to not work yet even with directly inlined checks, so it probably shouldn't be in scope for this suggestion.

Narrowing a property does not change the parent type. See #42384 for more information.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Jun 20, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants