Skip to content

Generic Extract alone cannot be used from uninstantiated generic parameterΒ #53998

@erwanvivien

Description

@erwanvivien

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

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 TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions