Giter Club home page Giter Club logo

Comments (7)

jcalz avatar jcalz commented on August 21, 2024 1

SO question

from typescript.

Andarist avatar Andarist commented on August 21, 2024

The first one doesn't really infer F. It only looks like it got inferred because that's the only kind you have in URItoKind. What you might see as "inferred" there is just the constraint.

The second one is different because at the time that you get to inferring A and B TypeScript has already settled on what F is (just based on the constraint). In there, the signature that goes through inference is <A, B>(f: (a: A) => Maybe<B>) => Maybe<B>. You can see how the return type of that callback is already instantiated to Maybe<B> and it's not a deferred generic indexed access (Kind<F, B>) like in the first one.

from typescript.

daniel-sima-abra avatar daniel-sima-abra commented on August 21, 2024

You can add readonly ['Array']: Array<A>; into URItoKind and it still works the same way.

from typescript.

Andarist avatar Andarist commented on August 21, 2024

Yes, you can see now how the first function doesn't infer "Maybe" for F here either. My point is that those two examples are very different. One deals with a deferred generic indexed access. The compiler is still trying to figure out what F could be (and it even fails to do it altogether). The other one doesn't suffer from this problem because F is already instantiated in a sense. So this time the compiler infers into a union type.

Your post implied that both are largely the same but they are not. I'm not saying that it's impossible to improve this - that I don't know ;p but those two situations deal with a different level of complexity. My educated guess is that if it would have to be improved it would have to be done here

from typescript.

daniel-sima-abra avatar daniel-sima-abra commented on August 21, 2024

Thank you for your explanation. I guess my question is, if this is an expected behavior or a bug, and if there is anything I can do right now to make test1 work.

from typescript.

RyanCavanaugh avatar RyanCavanaugh commented on August 21, 2024

While it would be nice for this to work, we'd need #30134 to make this work in the general case where one type parameter depends on the resolution of the previous one. The current algorithm isn't well equipped to insert an extra round of inference here to settle on F before trying to figure out B, and it's not super apparent to me that it'd be tractable to do so (consider the case where URItoKind is very large).

The example is a bit under-specified to give more advice. As written it seems kind of ambiguous, e.g. test2 works but gives different answers depending on the order of Maybe1 and Maybe2.

interface URItoKind<A> {
  readonly ['Maybe1']: Maybe<[A, unknown]>;
  readonly ['Maybe2']: Maybe<[unknown, A]>;
}
type URIS = keyof URItoKind<any>;
type Kind<URI extends URIS, A> = URItoKind<A>[URI];

class Maybe<A> {
    _inferHelper!: A;
  static of: <A>(a: A) => Maybe<A>;
}

declare function test1<F extends URIS, A, B>(f: (a: A) => Kind<F, B>): [F, B, Maybe<B>];
declare function test2<F extends URIS>(): <A, B>(f: (a: A) => Kind<F, B>) => [F, B, Maybe<B>];

declare const func: (x: number) => Maybe<[string, number]>;

const t1 = test1(func); // Maybe<unknown> - Doesn't work

const t2 = test2()(func)[2];

from typescript.

typescript-bot avatar typescript-bot commented on August 21, 2024

This issue has been marked as "Design Limitation" 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.