Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Types with index signatures lose all attribute signatures when using keyof or Omit #43478

Closed
nuintun opened this issue Apr 1, 2021 · 4 comments

Comments

@nuintun
Copy link

nuintun commented Apr 1, 2021

Bug Report

企业微信截图_16172737442928

💻 Code

interface Route {
  name: string;
  path: string;
  href?: string;
  exact?: boolean;
  strict?: boolean;
  [key: string]: string | boolean | undefined | object;
}

type RouteNode = Omit<Route, 'name'>;

const route: RouteNode = {} as RouteNode;

route.p;

🙁 Actual behavior

Lose all attribute signatures

🙂 Expected behavior

Do not lose attribute signatures

@MartinJohns
Copy link
Contributor

MartinJohns commented Apr 1, 2021

This is working as intended.

What search terms did you use to look for existing issues? omit index would already suffice.

@nuintun
Copy link
Author

nuintun commented Apr 1, 2021

@MartinJohns

I wrote a tool that converts some properties of an object into optional, but it doesn't work.

type PartPartial<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;

interface Route {
  name: string;
  path: string;
  href?: string;
  exact?: boolean;
  strict?: boolean;
  [key: string]: string | boolean | undefined | object;
}

type RouteNode = PartPartial<Route, 'name'>;

And the same PartRequired doesn’t work either.

type PartRequired<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;

But PartRequired like this is work well.

type PartRequired<T, K extends keyof T> = T & Required<Pick<T, K>>;

@MartinJohns
Copy link
Contributor

See the existing issues: #43139, #40999, etc. Omit<T> is using keyof T, and keyof T for types with indexers always results in string | number, so your individual properties are unavailable. This is working as intended, see #42436.

Issue #41966 mentions a workaround for this behavior (by excluding the index signature from the type).

@nuintun
Copy link
Author

nuintun commented Apr 1, 2021

@MartinJohns 3Q~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants