Comments (8)
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.
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.
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"
Did #53907 do this somehow?
from typescript.
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.
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.
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.
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.
Apart from that, it's a bit odd that
b
is considered to have typestring
, yet the compiler "knows" it has type"1 2"
in the evaluation ofc
.
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)
- Isolated declarations does not handle object getters and setters with different types HOT 6
- TypeScript does not support bigint literals in type literal keys HOT 1
- Isolated declarations uses type information when emitting computed object keys HOT 6
- Error: Debug Failure when importing `AssertionError` from `node:assert/strict` HOT 4
- transpileDeclaration - Errors on unresolvable globals HOT 2
- duplicate comments on decorated fields
- Type narrowing with union types create impossible case with else statement HOT 6
- Error when using `import()` type within JSDoc tag `@implements`
- Narrowring a readonly class with instanceof of does not work on the else branch HOT 1
- Disposables disposed of, even though they are passed to a wrapper and still in use HOT 3
- It's the JSON validator that complains here. HOT 4
- Isolated declarations inconsistent about banning default assignment patterns for function parameters HOT 2
- Isolated declarations uses type information to emit return type for async function HOT 2
- Inconsistency with Rest Parameter in Function Type HOT 2
- 2024 便宜机场推荐:便宜机场整理
- Why is Promise<void> accepted as void HOT 3
- Hoisted vars in blocks at module scope generate in invalid `d.ts` files when referenced using `typeof` HOT 1
- Isolated declarations allows return types that reference local expr or type
- Use of intersection type leads to extremely slow typecheck times HOT 9
- Allow leading underscore for types to bypass noUnusedLocals warning
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typescript.