Git Product home page Git Product logo

Comments (3)

RyanCavanaugh avatar RyanCavanaugh commented on September 27, 2024 1

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.

ShacharHarshuv avatar ShacharHarshuv commented on September 27, 2024

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.

typescript-bot avatar typescript-bot commented on September 27, 2024

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)

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.