Giter Site home page Giter Site logo

Comments (8)

Andarist avatar Andarist commented on June 3, 2024 1

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

from typescript.

fatcerberus avatar fatcerberus commented on June 3, 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.

from typescript.

jcalz avatar jcalz commented on June 3, 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?

from typescript.

Andarist avatar Andarist commented on June 3, 2024

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
}

from typescript.

Andarist avatar Andarist commented on June 3, 2024

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.

from typescript.

ahejlsberg avatar ahejlsberg commented on June 3, 2024

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.

from typescript.

ahejlsberg avatar ahejlsberg commented on June 3, 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.

from typescript.

fatcerberus avatar fatcerberus commented on June 3, 2024

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!

from typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

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

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.