-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Closed
Copy link
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue
Milestone
Description
Bug Report
π Search Terms
- Extract
- Generic
- Type union
π Version & Regression Information
- This is a bug
- This is the behavior in every version I tried, even nightly
β― Playground Link
π» Code
type ApiPost =
| {
path: "/something1";
body: {};
}
| {
path: "/something2";
body: { name: string; };
}
type PostPath = ApiPost["path"];
type PostBody<PATH extends PostPath> = Extract<ApiPost, { path: PATH }>["body"];
// type PostBody<PATH extends PostPath> = Extract<ApiPost, { path: PATH }>["body"] & []; // works
// type PostBody<PATH extends PostPath> = Extract<ApiPost, { path: PATH }>["body"] & object; // works
// type PostBody<PATH extends PostPath> = Extract<ApiPost, { path: PATH }>["body"] & { weird: "bug" }; //works
// type PostBody<PATH extends PostPath> = (ApiPost & { path: PATH })["body"] // works
const post = async <PATH extends PostPath>(
path: PATH,
{body, ...options}: Omit<RequestInit, 'body'> & {body: PostBody<PATH>}
): Promise<null> => {
return null
}
const tmp = <PATH extends PostPath>(
path: PATH,
body: PostBody<PATH>
) => {
post<PATH>(path, { body })
}π Actual behavior
It brings an error where it SHOULD accept the type, adding random types makes the error disappear
π Expected behavior
I should be able to use the first type as it does the same as every other types
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue