Giter VIP home page Giter VIP logo

Comments (7)

RyanCavanaugh avatar RyanCavanaugh commented on May 5, 2024 1

You can use a helper type if you want to clean up the display a little, but this does what you want:

type WithHelper = { [K in API['method'] as `${K}:${(API & { method: K })['path']}`]: API & { method: K } };

from typescript.

whzx5byb avatar whzx5byb commented on May 5, 2024

IMO it makes no sense to use infer in an infer constraint. Are there some real world code examples?

from typescript.

fatcerberus avatar fatcerberus commented on May 5, 2024

This is like, two levels of constraint-solving. I would never expect the compiler to be able to do this, and even if it could, there isn't just one correct answer (in particular, unknown is a valid solution for I2).

from typescript.

RyanCavanaugh avatar RyanCavanaugh commented on May 5, 2024

It's not wrong and there doesn't seem to be any reason to write it this way in the first place.

from typescript.

mjy9088 avatar mjy9088 commented on May 5, 2024

In my case:

type XHRMethod = "GET" | "POST"

// example
type API = { method: "GET", path: "/test" } | { method: "POST", path: "/test" };

/*
type ItWorks = {
    "GET:/test": {
        method: "GET";
        path: "/test";
    };
} | {
    "POST:/test": {
        method: "POST";
        path: "/test";
    };
}
*/
type ItWorks =
  API extends infer I
  ? I extends {
    method: infer IMethod extends XHRMethod;
    path: infer IPath extends string;
  }
  ? { [K in `${IMethod}:${IPath}`]: I }
  : never
  : never
  ;

/*
type ItDoesntWork = {
    [x: `GET:${string}`]: API;
    [x: `POST:${string}`]: API;
}
*/
type ItDoesntWork =
  API extends infer I extends {
    method: infer IMethod extends XHRMethod;
    path: infer IPath extends string;
  }
  ? { [K in `${IMethod}:${IPath}`]: I }
  : never
  ;

Why I need redundant infer I:

type XHRMethod = "GET" | "POST"

// example
type API = { method: "GET", path: "/test" } | { method: "POST", path: "/test" };

/*
type WithoutInferI = {
    "GET:/test": API;
    "POST:/test": API;
}
*/
type WithoutInferI =
    API extends {
        method: infer IMethod extends XHRMethod;
        path: infer IPath extends string;
    }
    ? { [K in `${IMethod}:${IPath}`]: API }
    : never
    ;

/*
type WithInferI = {
    "GET:/test": {
        method: "GET";
        path: "/test";
    };
} | {
    "POST:/test": {
        method: "POST";
        path: "/test";
    };
}
*/
type WithInferI =
    API extends infer I ? I extends {
        method: infer IMethod extends XHRMethod;
        path: infer IPath extends string;
    }
    ? { [K in `${IMethod}:${IPath}`]: I }
    : never : never
    ;

I don't think this feature is necessary, but it could improve the code in some cases.

And I think, without it, is somewhat unintuitive.

I'm really not sure about this: is implementing this feature impossible or not suitable for the purpose?

I would appreciate any guidance on this :)

from typescript.

mjy9088 avatar mjy9088 commented on May 5, 2024

It works on the example where one method has one path...

However, { [K in ... as ...]: ... } seems super useful for me! I could not have thought of that. Thanks 😄😄😄

from typescript.

typescript-bot avatar typescript-bot commented on May 5, 2024

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.

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.