question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Infer literal types for string concatenations and similar expressions

See original GitHub issue

TypeScript Version: 2.1.5

Expected behavior:

const foo = 'world'; // value type: 'world'
const bar = `hello ${foo}`; // value type: 'hello world'

Actual behavior:

const foo = 'world'; // value type: 'world'
const bar = `hello ${foo}`; // type: 'string'

Motivation

For constants which are created from other constants the IntelliSense is often more useful, when we see its value type instead of the “real” type.

More practical example:

const fooBreakpoint = '800px';
const fooMediaQuery = `@media(min-width: ${fooBreakpoint})`;

When fooMediaQuery is used I’d like to see @media(min-width: 800px) as its type instead of string.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:17
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
bradzachercommented, Nov 18, 2022
1reaction
fjmorelcommented, Apr 12, 2018

I’d like to be able to do something like this, building a key using string literals:

type Obj = Record<"a" | "a1" |  "b" | "b1", string>;

function getKey(useA: boolean, useOne: boolean): keyof Obj {
  // [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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Template Literal Types - TypeScript
When used with concrete literal types, a template literal produces a new string literal type by concatenating the contents.
Read more >
Is there currently anyway to concatenate two or more string ...
Template string types provide the ability to concatenate literal strings, convert literals of non-string primitive types to their string representation, and ...
Read more >
PEP 586 – Literal Types - Python Enhancement Proposals
This PEP proposes adding Literal types to the PEP 484 ecosystem. Literal types indicate that some expression has literally a specific value.
Read more >
I need to learn about TypeScript Template Literal Types
TypeScript 4.1 can concatenate strings in types using the same syntax from JavaScript: type World = "world"; type Greeting = `hello ...
Read more >
Addition of string (or template) literal types should result in a ...
The templates are probably the "similar expressions", but the title also mentions "literal types for string concatenations" (also in several ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found