Comments (3)
I'm confused because you know about const
but aren't using it where doing so would give you the result you want.
Inference is presented with a set of candidates plus a constraint and tries to figure out what you wanted. This talk goes into some examples - it doesn't talk about literals but you can see from that talk why inference makes some decisions but not others.
Why adding extends string means that the type is now inferred as const?
Because if you wanted the function to return string
when passed "hello"
, you wouldn't have written the constraint in the first place.
Why using an array changes the behavior back to not using as const?
Because the prior argument no longer applies (e.g. you might have a subtype of array that you're specializing to)
Why booleans gets the const treatment while strings don't?
Booleans are a union but strings aren't
from typescript.
I'm confused because you know about const but aren't using it where doing so would give you the result you want.
I'm not sure how I'm supposed to get the result I want using const
, as it seems to be the opposite of what I want.
Imagine this example: function f<T extends number | string>(value: T): T
. How do I make it infer f('hello')
as string
, f(1)
as number, and f<'red' | 'blue'>('red') as 'red' | 'blue'
?
In that case, there is a reason to the constraint - I do want to narrow number | string
but I want to narrow it to either number
or string
.
The only solution I was able to figure out is using overloads instead:
function f(value: string): string;
function f(value: number): number;
function f(value: string | number): string | number;
However, this doesn't work for more complicated examples. Consider this:
function f<T extends (object | boolean)[]>(value: T): T;
I would expect f([{name: 'hello', true}])
to be inferred as ({name: 'hello'}|boolean)[]
but it is inferred as ({name: 'hello'}|true)[]
.
Is it possible to type in a way that this would work?
from typescript.
This issue has been marked as "Question" and has seen no recent activity. It has been automatically closed for house-keeping purposes.
from typescript.
Related Issues (20)
- CommonJS module output broken after updating from 5.5.4 to 5.6.2 HOT 7
- Check when a function that uses `this` is referenced without binding it HOT 2
- Built-in URL is not compatible with `@types/node` URL
- [suggest] constructor init delegation with "strictPropertyInitialization" HOT 2
- Potential memory leak or dead recursive during auto completion HOT 2
- Incorrect type deducted for accumulator in reduce HOT 2
- moduleResolution: 'bundler' with subpath import seem to not support directory module and extensionless import HOT 4
- Source mappings are missing for serialized properties
- Missing overload method on union types HOT 7
- JSDoc `@import` of "tslib" fails under `--moduleResolution bundler`
- Interface that extends another no longer constrains types like the original HOT 1
- Proposal: Allow isolated declarations to infer results of constructor calls HOT 10
- Property 'difference' does not exist on type 'Set' HOT 3
- Type narrowing of condition leads to unexpected error for assignment HOT 3
- Increase the distinction between class and interface properties when defining a class or interface
- Add adaptation to class and interface property prompts HOT 1
- Weird behavior with recursive generic types HOT 2
- Array of unions changed to union of arrays in conditional type HOT 4
- tsserver requires `npm` to be installed on `neovim` trough `mason` 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.