Accepting generic component as an argument #560
-
I have
Unfortunatelly I am not sure how to type this out or if this is even supported.
I would appreciate some help on this if there is a better way to do this, or is this a bug in glint? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Unfortunately this is something that there isn't quite a direct way to express to TypeScript. Taking a slightly simpler example: declare const ComponentA: ComponentLike<{
Args: {
name: string;
age: number;
};
}>;
interface MyComponentSignature<T extends typeof ComponentA> {
Args: { item: T };
Blocks: {
default: [item: WithBoundArgs<T, 'name'>];
};
} The issue you're running into is that Note that this isn't in any way specific to Glint, and is just a general property of how TypeScript's structural typing works: // Given a function like this:
declare function runLater<T extends (args: { foo: string; bar: number }) => void>(callback: T): void;
// A callback that accepts every declared arg is valid to pass:
runLater(({ foo, bar }) => {
console.log(foo, bar);
});
// But so is one that only uses a subset of them:
runLater(({ bar }) => {
console.log(bar);
});
// Or even one that doesn't accept any args at all:
runLater(() => console.log('hi')); Because of these kinds of ambiguities, TypeScript will frequently be unable to perform complex type operations on generic type parameters like I imagine your real situation is a bit more complicated than the simplified version in your example, but it looks like you're passing If that's not viable, you may just need to fall back to |
Beta Was this translation helpful? Give feedback.
-
Oh I see, thank you for the detailed explanation @dfreeman, really appreciate that fast reply and the effort you put into the explanation. |
Beta Was this translation helpful? Give feedback.
Unfortunately this is something that there isn't quite a direct way to express to TypeScript. Taking a slightly simpler example:
The issue you're running into is that
ComponentA
isn't the only possible thingT
could be there. A component that only accepted a numeric@age
arg, or that didn't require any arguments at all, would be equally valid.Note that this isn't in any way specific to Glint, and is just a general property of how TypeScript's str…