Skip to content
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

Computed string literal can't be used as generic argument #58494

Closed
DimaIT opened this issue May 10, 2024 · 8 comments Β· Fixed by #58500
Closed

Computed string literal can't be used as generic argument #58494

DimaIT opened this issue May 10, 2024 · 8 comments Β· Fixed by #58500
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@DimaIT
Copy link

DimaIT commented May 10, 2024

πŸ”Ž 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

@fatcerberus
Copy link

fatcerberus commented May 10, 2024

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.

@jcalz
Copy link
Contributor

jcalz commented May 10, 2024

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?

@Andarist
Copy link
Contributor

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

@Andarist
Copy link
Contributor

My PR is definitely related but it's not the whole story. I'll bisect the other bit more later but I wouldn't be surprised if that would point to some other PR of mine πŸ˜…

The OP's case changed between 5.1 and 5.2. #53907 was included as part of 5.1 already and it accidentally changed @jcalz's examples and things like this:

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

enum E {
  A = b, // "1 2", wat
}

@Andarist
Copy link
Contributor

Oh, somehow I misinterpreted some things at first. #53907 was actually included in 5.2 and is responsible for @DimaIT's and @jcalz's issues. It reused the evaluation strategy that was already in place. It was introduced in #50528 and is responsible for the enum example from my post above.

@ahejlsberg
Copy link
Member

The issue with #53907 is that it only performs constant evaluation when the criteria for creating a template literal type are not satisfied (i.e. constant evaluation only occurs in situations where the type would otherwise have been string). That's a bit backwards. We need to first check if the template literal expression can be evaluated as a constant, and, if so, return a string literal type. I will put up a PR that fixes.

@ahejlsberg ahejlsberg self-assigned this May 11, 2024
@ahejlsberg
Copy link
Member

ahejlsberg commented May 11, 2024

Another example of the strangeness:

const a = '1';                // "1"
const b = a + ' 2';           // string
const c = `${b} 3`;           // "1 2 3"
const d = `${b} 3` as const;  // `${string} 3`

It definitely doesn't make sense for d to be a less specific type than c, and that, combined with the phasing of type inference and argument type checking in the call fn(`${b} 3`), is what causes the issue.

Apart from that, it's a bit odd that b is considered to have type string, yet the compiler "knows" it has type "1 2" in the evaluation of c. We may want to consider constant evaluation of + applied to strings, but that's a separate issue.

@ahejlsberg ahejlsberg added this to the TypeScript 5.5.1 milestone May 11, 2024
@ahejlsberg ahejlsberg added Bug A bug in TypeScript Fix Available A PR has been opened for this issue labels May 11, 2024
@fatcerberus
Copy link

Apart from that, it's a bit odd that b is considered to have type string, yet the compiler "knows" it has type "1 2" in the evaluation of c.

Yeah, that threw me for a loop too. I know TS has a concept of "widening" types where a literal type can be widened to its base primitive type, but this is the first time I've ever seen a "narrowing" type!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants