Skip to content

Inheritance messes with comparison to primitive #48565

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
CALICAWESOME opened this issue Apr 5, 2022 · 2 comments
Closed

Inheritance messes with comparison to primitive #48565

CALICAWESOME opened this issue Apr 5, 2022 · 2 comments

Comments

@CALICAWESOME
Copy link

CALICAWESOME commented Apr 5, 2022

Bug Report

Typescript version 4.6.2
VSCode version 1.65.0

🔎 Search Terms

#202
#25642

💻 Code

1 type Foo = String & { bar: 'baz' }
2
3 function qux(quux: Foo) {
4   const quuz = '';
5   quux === quuz;
6 }
7
8 qux('');

🙁 Actual behavior

Line 5:
This condition will always return 'false' since the types 'string' and 'Foo' have no overlap.

🙂 Expected behavior

No compiler error

@Josh-Cena
Copy link
Contributor

Josh-Cena commented Apr 5, 2022

String is a wrapper type of string, and in general Stringstring, because typeof new String("foo") === "object" while typeof "foo" === "string".

FYI, there's the TS-ESLint rule ban-types that bans the String type because of this confusion.

@CALICAWESOME
Copy link
Author

Wow. Thanks for the quick response! That did it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants