5 ms·
Last I checked it was impossible to parametrize component type. In React you can write `<MenuSelect<User> users={...} />`.
by dhucerbin 6y ago
Last I checked it was impossible to parametrize component type. In React you can write `<MenuSelect<User> users={...} />`.
- patwoz 6y agoPlease don't. It looks awful. And I think it's not necessary. Typescript should inherit it's type depending on what you are passing into "users".
- efrafa 6y agoThere are valid reason to do that. Like a component that takes render function and item that is passed into it that can be anything.
- patwoz 6y agoYou can do that, but you don't have to. See my example here at line 26: https://codesandbox.io/s/clever-neumann-89tnx?file=/src/App.tsx https://codesandbox.io/s/clever-neumann-89tnx?file=/src/App.... Typescript will inherit the type automatically.
- kecupochren 6y agoThat's amazing, thanks for the tip
- dhucerbin 6y agoI assume you don’t like invocation? That’s not the point here. In svelte you can’t* define type variable that’s bound to the same type in whole component. I can’t enforce that properties `items` is `T[]` and `selectedItem` is `T`. * Last time I checked. I could be wrong now.
- patwoz 6y agoI'm writing about <MenuSelect<User users={..} /> and not about Svelte. I don't know anything about Svelte except what I just learned in the article. I just don't like the syntax of <MenuSelect<User> /> and favour <MenuSelect /> instead, because Typescript will inherit it's generic type from users={...} See my example here: https://codesandbox.io/s/clever-neumann-89tnx?file=/src/App.tsx https://codesandbox.io/s/clever-neumann-89tnx?file=/src/App....
- Palmik 6y agoI am not sure I understand. In the `MenuSelect` component you can declare `export let users: Array<User>;` and then pass into `users` anything that has the `User` interface.