Comments (10)
Ah, ye - totally. I certainly didn't want to imply that this is a bug - just why this is different today at the implementation level.
from typescript.
type R6 = 1 & { a: 1 } extends 1 & infer U ? U : never // { a: 1 }
type R7 = 1 & { a: 1 } extends number & infer U ? U : never // 1 & { a: 1 }
inferFromMatchingTypes
gets passed isTypeIdenticalTo
here as matches
so I bet that it's the reason why those behave differently.
from typescript.
fwiw, technically the inference for R7
is not wrong - number & 1 & { a: 1 }
still describes the type accurately. I don’t consider infer
to be an exact science; it’s a bit like solving an equation in that there can be multiple valid solutions—just that in this case the compiler can only pick one.
from typescript.
What would “typeof T
for types” even mean? Given that typeof x
specifically means “give me the type assigned to the thing called x
in value-space” and you can’t console.log
a type…
from typescript.
What would “typeof T for types” even mean?
I'm still articulating that. Basically it returns string literal types like number
, string
, stringTemplateLiteral
etc.
Then you can do typeof T extends 'numberLiteral' extends true ? ... : ...
It should be relatively easy to do inside TS as it has that info in the AST.
The problem of that approach is how to handle intersection types.
Another way is to provide type utils like IsStringTemplateLiteral<T>
like what I have in type-plus
.
There are some variants need to be handled such as distributive
(IsString<number | string, { distributive: true }> -> boolean
, IsString<number | string, { distributive: false }> -> false
), and exact
(IsString<'abc', { exact: true }> -> false
), and special types (any
| unknown
| void
| never
) handling.
from typescript.
understand. It depends on what is the definition of infer
in terms of how does it consider and interact with sets.
The bottomline for me is whether if we have a not-too-hacky-way to build the types we want.
from typescript.
The general behavior of T & infer U
is that it'll match exactly T & V
to U = V
, but otherwise for S & V
, you'll get U = S & V
. Both are correct bounds.
from typescript.
Understand that this is not a defect. It's the same as @Andarist pointed out above.
Maybe the question is how it should behave when it comes to sets like number vs numeric literals, string is string literals, boolean vs true/false.
The question is twofold: consistency and application limitation.
At the moment, this infer
behavior creates inconsistency compare to how conditional type works:
type P1 = number & { a: 1 } extends number ? true : false // true
type P2 = 1 & { a: 1 } extends number ? true : false // true
type P3 = number & { a: 1 } extends number & infer U ? U : never // { a: 1 }
type P4 = 1 & { a: 1 } extends number & infer U ? U : never // 1 & { a: 1 }
Yes, we can explain that the implementation is using exact match, but that does not help user to reason the type. To be consistent, both extends
and infer
should perform exact match or act based on set.
Using 'a' & { a: 1 } extends 'a' & infer U ? U : never
to extract { a: 1 }
is not very practical.
If there is an alternative way to do that, or have a way to address the string template literal issue mentioned in the OP #54648, that would work.
from typescript.
This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes.
from typescript.
Please consider re-open this. The inconsistency makes it not possible to write reusable generic types that work with intersection types.
from typescript.
Related Issues (20)
- A Pragmatic, Not-Really-Typed Errors Proposal HOT 11
- Make switch exhaustiveness check as a configuration of tsc. HOT 1
- Cannot extend `XMLHttpRequest` when target is `ES5` HOT 2
- Type predicate not inferred for discriminated union?
- Compilation error when reassigning destructurig array variable names HOT 1
- Operator '<' ***CAN*** be applied to types 'number | BigInt' and 'number' HOT 8
- JSDoc typedef imports are emitted with a resolved path to node_modules HOT 1
- Incorrect Path Resolution for Type Imports in TypeScript Declarations HOT 2
- All primitives can be spread in objects according to MDN but TypeScript prevents it HOT 15
- Go-to-definition should only show relevant definitions when value & type have same name HOT 6
- ArrayBuffer.isView not narrowing type HOT 3
- Update import/export specifier sort on rename HOT 7
- TS can't automatically infer type from outer function in certain cases HOT 7
- Allow non-polymorphic ("private") extension of base classes HOT 4
- Design Meeting Notes, 3/26/2024
- Shorter tuple contravariant inference gets picked over longer covariant inference when both come from rests
- Type widened prematurely in `catch` HOT 2
- Inconsistency with Template Literal Types and Conditional Types HOT 1
- Uninitialized instance properties can be accessed via initializers HOT 2
- No duplicate identifier error issued if `const` declared with function type with expandos HOT 2
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.