microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

Home Page:https://www.typescriptlang.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Computed string literal can't be used as generic argument

DimaIT opened this issue ยท comments

๐Ÿ”Ž Search Terms

"string literal is not assignable to parameter of type", "generic string literal argument"

๐Ÿ•— Version & Regression Information

  • This changed between versions 5.1.6 and 5.2.2

โฏ Playground Link

https://www.typescriptlang.org/play/?#code/GYVwdgxgLglg9mABMMAeAKgPgBQEMBOA5gFyLoCUp6iA3gFCKOL4CmUI+SBhA3HQL506EBAGcoiXIgC8iAOQBGOXxFhxiAEYzJiANTzEAJmVCU2AAYASGhv6IAzOfI8gA

๐Ÿ’ป Code

function fn<T>(arg: T): T {
    return arg;
}

const a = '1';
const b = a + ' 2';

fn(`${b} 3`);

๐Ÿ™ Actual behavior

Argument of type '`${string} 3`' is not assignable to parameter of type '"1 2 3"'.

๐Ÿ™‚ Expected behavior

No errors are expected

Additional information about the issue

No response

Thatโ€™s bizarre - itโ€™s inferring T = "1 2 3" from the argument, but then deciding the same argument isnโ€™t assignable to the type it just inferred.

I... what?! Somehow TS is #44905 magically almost kind of happening? What gives?

const x = "w" + "t" + "f";
// const x: string
const y = `${x}` 
// const y: "wtf"

Playground link

Did #53907 do this somehow?

The OP's repro is quite bizarre ๐Ÿ˜ฑ

Did #53907 do this somehow?

That was meant to be my guess but - obviously - you are already one step ahead :D