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

const wrong = () => (1 < 3) as true // type: () => true this should be an error. #58636

Closed
jko206 opened this issue May 23, 2024 · 9 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@jko206
Copy link

jko206 commented May 23, 2024

πŸ”Ž Search Terms

  • typescript bug return type true
  • () => true bug

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "typescript bug return type true"

⏯ Playground Link

https://

πŸ’» Code

const wrong = () => (2 < 3) as true

πŸ™ Actual behavior

This doesn't trigger a TS error.

πŸ™‚ Expected behavior

It should.

Additional information about the issue

I know this would be a hard challenge to solve, but I thought you guys should at least know about it.

@dragomirtitian
Copy link
Contributor

Maybe I'm just slow today, but why should this trigger an error?

@fatcerberus
Copy link

@dragomirtitian This is just a guess but maybe OP expects an error because 2 < 3 is always false? But then false as true isn’t an error either so…

@jko206
Copy link
Author

jko206 commented May 23, 2024

I should have been more clear.

as @fatcerberus mentioned, I was expecting the above code to be an error, since the 1 < 3 is always false.

@whzx5byb
Copy link

Note that all of these are valid...

const a = false as true;
const b = 'x' as 'y';
const c = 1 as 2;

@KDwevedi
Copy link

...I was expecting the above code to be an error, since the 1 < 3 is always false.

Why is 1 < 3 false? < is just the less than comparison operator, correct?

@MartinJohns
Copy link
Contributor

This is working as intended. Using a type assertion you tell the compiler "shh.. trust me, it's this type, I know what I'm doing..."

@KDwevedi They surely mean the 1 > 3 case.

@jcalz
Copy link
Contributor

jcalz commented May 24, 2024

I'm amazed that people figured out what this issue meant. My brain was imploding trying to understand what could possibly have been the bug: "Why would the () => part matter here? Why is the desired behavior just 'an error' without mentioning, like, what kind of error? Why wouldn't you be allowed to narrow from boolean to true in a type assertion? Wait, has 3 cut in front of 1 and 2 in the number line again? Am I... am I dreaming? This is a dream, isn't it?"

@fatcerberus
Copy link

3 is always cutting in front of 1 and 2, he thinks he's so important having his visage plastered everywhere. Still better than that jerk 7. 6 still needs therapy to this day after witnessing t h e i n c i d e n t

@ahejlsberg ahejlsberg added the Working as Intended The behavior described is the intended behavior; this is not a bug label May 24, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Working as Intended" 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 May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

9 participants