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

Function which accepts a runtype and omits some fields #314

Open
okomarov opened this issue Nov 7, 2022 · 1 comment
Open

Function which accepts a runtype and omits some fields #314

okomarov opened this issue Nov 7, 2022 · 1 comment

Comments

@okomarov
Copy link

okomarov commented Nov 7, 2022

Minimal example

Here's a named book

const NamedBook = Record({
  name: String,
  title: String
});

I want to create a type with the id in place of the name, so I can manually do this:

const Book = NamedBook.omit('name').extend({ id: String })

Q: Since I have 15+ runtypes, how can I write a generic function for that? I tried:

function extendWithId<T extends { name: typeof String }>(r: Record<T, false>) {
  return r.omit('name').extend({ id: String });
}

const Book = extendWithId(NamedBook);

but I get:

Argument of type '["name"]' is not assignable to parameter of type '"name"[] extends (keyof T)[] ? (keyof T)[] & "name"[] : never[]'.ts(2345)

@yuhr
Copy link
Collaborator

yuhr commented Nov 9, 2022

Thanks for reporting, it seems like a limitation of TS's type inference but I'll try to seek for some solution.

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