Comments (3)
Inference is based on a set of heuristics. Currently, TS eliminates matching types here (based on isTypeIdenticalTo
) and when either targets or sources are emptied... it returns. Later on, it picks up the constraint of your type (unknown
) variable in the absence of inference candidates.
This is a "correct answer" since unknown & string
is still string
from typescript.
For example, in the following two examples, I still can't know why the result of the first example is unknown instead of string, and why I can't get a more precise type like the second example.
Where did the targets or sources and inference candidates you mentioned come from.
type MyType<T> = T extends (infer R) & string ? R : never
type Result = MyType<string> // unknown
type MyType<T> = T extends (infer R) & string ? R : never
type Result = MyType<'abc'> // abc
from typescript.
It's just how TypeScript works today. It has some algorithm for matching between your source (type argument here) and the targets (the intersection here). There is no better answer to that - it's an implementation detail. It's not incorrect though.
Those examples aren't really practical, so I'm not sure what you're trying to solve here. I understand that it might be confusing and interesting why they behave differently but that's about it. I'm sure that this part of the algorithm was written to improve some more real-life scenarios for which that selection mechanism makes sense more often than not.
from typescript.
Related Issues (20)
- Allow customization of type display format in hover tooltips HOT 4
- Debug Failure caused by Invalid syntax(null) HOT 1
- Debug failure caused by incorrect identifier and undefined type
- TS2416 nonsensical fix suggestion (and possible false positive?) in a generic class implementing an interface verbatim HOT 3
- lib.es5.d.ts: Default ArrayConstructor generic parameter to `undefined` HOT 2
- Literal Tuples/'as const' - allow lib creator to mark type parameters as 'as const' to allow for better TS DSLs HOT 2
- [NewErrors] 5.6.0-dev.20240721 vs 5.5.3 HOT 34
- Shape-based type equivalence should evaluate getters and setters, not just getters HOT 7
- [ServerErrors][JavaScript] 5.6.0-dev.20240721 vs 5.5.3 HOT 13
- [ServerErrors][TypeScript] 5.6.0-dev.20240721 vs 5.5.3 HOT 14
- Default type parameter values always shown on hover HOT 2
- Taking TypeScript for a spin for first time, errors for resizable ArrayBuffer HOT 7
- Shallow copy of Map class produce incorrect typings HOT 1
- Passing spreading array to function parameters should raise an error HOT 2
- `import defer` Stage 2.7 proposal support
- Types with optional props get implicit index signatures that cannot be made explicit HOT 1
- Presence of `export` affects narrowing HOT 3
- Mixin types are intersections rather than proper subclasses
- Duplicate keys do not error HOT 7
- JsDoc is missing/duplicated in declarations for overloads declared in classes declared in functions
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.