-
Notifications
You must be signed in to change notification settings - Fork 12.9k
String literal types #5185
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
String literal types #5185
Changes from 1 commit
311a0cf
911e907
f04cc39
191be4f
84786d8
dc0e368
8891fba
82545ce
ed927d8
a3e7ccb
20c2c4e
87f2957
f721971
7b4e94d
d8d72aa
315b06d
4b736da
fd5dec4
f7a6ac7
a440f06
d2e2a55
6e3343c
74ac57d
61ece76
84b64c4
3788254
ebc47d5
725bda8
ec0d49a
1dbd8d1
307d73e
049d02f
6618fd2
bf4880a
5e23143
8dbfe1c
a1fcfaf
bb232f7
f939ff2
d234b8d
c011ed4
38090c6
d294524
ea4e21d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10286,7 +10286,7 @@ namespace ts { | |
|
||
function checkStringLiteralExpression(node: LiteralExpression) { | ||
// TODO (drosen): Do we want to apply the same approach to no-sub template literals? | ||
|
||
let contextualType = getContextualType(node); | ||
if (contextualType) { | ||
if (contextualType.flags & TypeFlags.Union) { | ||
|
@@ -10435,7 +10435,7 @@ namespace ts { | |
case SyntaxKind.StringLiteral: | ||
return checkStringLiteralExpression(<LiteralExpression>node); | ||
case SyntaxKind.NoSubstitutionTemplateLiteral: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not that I want to sound negative but for every string literal, this gets called. It's a huge slowdown. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What kind of numbers are you seeing (and on what codebase)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think the VM already optimize for this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's useless to speculate about what's fast and what's not. Measure it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Meh. Measure it in 1 version of Chrome, it changes in the next. You measure it, I've said what I had to say. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jbondc 👍 for the benchmark test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think the original issue was so terrible. You only ran into this case when you were contextually typed by a type with a string literal constituent to begin with. In practice, this is not frequently encountered. However, @jbondc, I think you'll like the the current implementation a lot better. We now only create a string literal type if the constituent types have a string literal type, not just if their content is equal. Furthermore, the types are cached in a map of strings to string literal types. Whenever testing the assignability of two string literal types, reference equality kicks in (which is fast). Additionally, error messages are slightly better because you have a specific literal type to report (i.e. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jbondc at worst that's probably much faster than trying to resolve the 100 different overloads. Though, I'm going to try to avoid speculating. I wasn't seeing a real perf hit in our benchmarks even with the initial change. |
||
return stringType | ||
return stringType; | ||
case SyntaxKind.RegularExpressionLiteral: | ||
return globalRegExpType; | ||
case SyntaxKind.ArrayLiteralExpression: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we resolve this
TODO
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm choosing not to apply the same approach because we currently don't treat NoSubTemplates the same as strings in places like string indexing and overload resolution. I think it would be weird to treat them the same way here but not elsewhere.
For the record, I would prefer if we treated them the same elsewhere, but I don't want to conflate that with this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright.