GithubHelp home page GithubHelp logo

Comments (3)

Andarist avatar Andarist commented on July 25, 2024

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.

Zackin-He avatar Zackin-He commented on July 25, 2024

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.

Andarist avatar Andarist commented on July 25, 2024

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)

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.