You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Infer literal types for string concatenations and similar expressions #13969
When fooMediaQuery is used I'd like to see @media(min-width: 800px) as its type instead of string.
This is interesting, mostly because it sounds like you are leveraging your editor's support of string literal types so that you can quickly glance at a type and get its content.
Yeah, when I work with CSS-in-JS solutions like glamor it is so great to immediately see some concrete value for different constants instead of just number or string.
import{css}from'glamor';constcolor='#fff';constmq='@media(min-width: 800px)';constmyStyle=css({[mq]: {// hover over `mq` → see '@media(min-width: 800px)'
color // hover over `color` → see '#fff'}});
If infering the value type doesn't work (e.g. because of templated strings) I need to duplicate the information into a comment which of course easily gets out of date, if someone changes the values without changing the comment.
import{css}from'glamor';constcolor='#fff';constbreakpoint='800px';/** * The media query is '@media(min-width: 800px)'. */constmq=`@media(min-width: ${breakpoint})`;constmyStyle=css({[mq]: {// hover over `mq` → see 'string' as its type and the comment which shows the value
color // hover over `color` → see '#fff'}});
changed the title [-]Infer value type for easy expresssions[/-][+]Infer literal types for string concatenations and similar expressions[/+]on Jun 16, 2017
I'd like to be able to do something like this, building a key using string literals:
typeObj=Record<"a"|"a1"|"b"|"b1",string>;functiongetKey(useA: boolean,useOne: boolean): keyofObj{// [ts] Type 'string' is not assignable to type '"a" | "a1" | "b" | "b1"'.return(useA ? "a" : "b")+(useOne ? "1" : "");}
Adding a cast after the string concatenation makes the error go away, but doesn't actually check that my key generation is still valid if I change Obj.
Similar to Frederic Morel (@fjmorel), I'd love to have this distribute over a union. My use case is basically:
// i don't control the shape of x.constx={item1: true,item2: false,item3: true};([1,2,3]as(1|2|3)[]).forEach(itemNum=>{// would love type inference here as item1 | item2 | item3constitemKey=`item${itemNum}`;});
Activity
DanielRosenwasser commentedon Feb 9, 2017
This is interesting, mostly because it sounds like you are leveraging your editor's support of string literal types so that you can quickly glance at a type and get its content.
donaldpipowitch commentedon Feb 9, 2017
Yeah, when I work with CSS-in-JS solutions like glamor it is so great to immediately see some concrete value for different constants instead of just number or string.
If infering the value type doesn't work (e.g. because of templated strings) I need to duplicate the information into a comment which of course easily gets out of date, if someone changes the values without changing the comment.
[-]Infer value type for easy expresssions[/-][+]Infer literal types for string concatenations and similar expressions[/+]fjmorel commentedon Apr 12, 2018
I'd like to be able to do something like this, building a key using string literals:
Adding a cast after the string concatenation makes the error go away, but doesn't actually check that my key generation is still valid if I change Obj.
ethanresnick commentedon Dec 8, 2018
Similar to Frederic Morel (@fjmorel), I'd love to have this distribute over a union. My use case is basically:
ascott18 commentedon Aug 21, 2019
This would be fantastic to see for indexing into objects with string literals, especially now that const assertions have been added in 3.4.
vdh commentedon Jan 13, 2020
It'd be nice to be able to use either string literals or unions.
mortoray commentedon Jun 16, 2020
This would be helpful for string enums with a common prefix.
This is currently rejected with error
TS2553: Computed values are not permitted in an enum with string valued members.vdh commentedon Jul 27, 2020
Maybe a const assertion syntax would suit this?
jcalz commentedon Sep 3, 2020
this is not inferred yet, but now we at least have #40336 to represent string concatenation at the type level,
bradzacher commentedon Nov 18, 2022
Worth noting that if you add
as constthis should work now with template literal types!https://www.typescriptlang.org/play?#code/MYewdgzgLgBAZiEMC8MDkB3EAnANgEzQG4YB6UmANwENcBXAUxigE8AHBgLnSz0IChQkWACNq2FDAAGACwa5cSACQBvBCAC+UmNQgwh0Iv3IxTpgHoB+IA
jakebailey commentedon Apr 8, 2024
This was fixed in #53907, which I believe has enough testing for this.