Skip to content

Output from JavaScript operator typeof should be type-checked as a string #54887

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

Closed
4 of 5 tasks
patik opened this issue Jul 5, 2023 · 4 comments
Closed
4 of 5 tasks
Labels
Duplicate An existing issue was already created

Comments

@patik
Copy link

patik commented Jul 5, 2023

Suggestion

πŸ” Search Terms

List of keywords you searched for before creating this issue: typeof

βœ… 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

The traditional typeof operator (not the TS one!) returns a string, but TypeScript does not always type-check it as such:

// The compiler throws two exceptions for this:
// This comparison appears to be unintentional because the types 'string' and 'never[]' have no overlap.(2367)
// This condition will always return 'false' since JavaScript compares objects by reference, not value.(2839)
if (typeof foo === []) {
    //
}

// But the compiler does allow this, even though the lefthand side is a string and the righthand side is `undefined`
if (typeof foo === undefined) {
    // 
}

Playground

When strict equality is used, I would expect the compiler to also throw 2367 and 2839 when This seems to be the case for undefined, null, and {}.

πŸ“ƒ Motivating Example

This code is allowed, but I think TS should have caught it:

if (typeof window === undefined) {
    //
}

πŸ’» Use Cases

The output of typeof is often compared to other variables, and TS should make sure those variables are also strings, at least when using ===.

@Josh-Cena
Copy link
Contributor

Duplicate of #29200

@fatcerberus
Copy link

Also note this is not specific to typeof. typeof foo is in fact already typed as string; comparison of anything with undefined is just always allowed.

@Andarist
Copy link
Contributor

Andarist commented Jul 5, 2023

It would be fairly easy to improve this situation for noUncheckedIndexedAccess but I have no idea how to improve this outside of this setting. The problem is that this code has to work:

function parse(config: Record<string, number>) {
  if (config.a === undefined) {
  }
}

And we have no way of knowing if the left type in this comparison comes from the index signature or not. The check responsible for allowing those comparisons can be found in isTypeEqualityComparableTo

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 6, 2023
@typescript-bot
Copy link
Collaborator

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

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 9, 2023
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

6 participants