Comments (7)
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.
IMO it makes no sense to use infer
in an infer constraint. Are there some real world code examples?
from typescript.
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.
It's not wrong and there doesn't seem to be any reason to write it this way in the first place.
from typescript.
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.
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.
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)
- Allow overload signatures to have different access levels HOT 3
- Watcher recursively watches irrelevant directories HOT 13
- Auto-closing of tags within curly braces `{}` does not work when parent element is same tag in JSX HOT 1
- Undocumented breaking change in 5.5.0-beta for reference directives HOT 3
- 5.5.0 regression - importHelpers do not work with moduleResolution: bundler HOT 1
- 5.5.0 inheriting outDir: ${configDir} does not automatically exclude it from compilation HOT 3
- isolatedDeclarations wrongly enabled in VSCode HOT 3
- ESNext Set methods HOT 1
- isolatedDeclarations should not have warning for functions that have no return statements HOT 4
- Trivia ownership documentation seems to be incorrect
- `--isolatedDeclarations` allows generator functions HOT 1
- Allow `--noCheck` on the CLI with top-level `--build`
- Union in template literal simplifying unexpectedly HOT 5
- Default import from an ESM package into a CommonJS project with esModuleInterop is faulty HOT 3
- Watch cannot handle large amount of file changes HOT 1
- generator next type should be inferred as union (instead of intersection) of yields' types OR just unknown
- Elementwise elaboration fails to be found for properties mixed with incompatible index signatures using intersections
- Error when accessing properties on `globalThis` with the same name of a global variable HOT 2
- [NewErrors] 5.5.0-dev.20240428 vs 5.4.5 HOT 49
- Private class properties prevent `Readonly` type inference 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.