Generic types with HelperLike
#542
-
Thanks for your work on this! 😄 I'm trying to type some external addons that provide helpers. For example: import { HelperLike } from '@glint/template';
declare module '@glint/environment-ember-loose/registry' {
export default interface Registry {
or: HelperLike<{
Args: { Positional: [any, any] };
Return: any;
}>;
}
} The helper can be invoked like this and it returns either of the two arguments (for example): However, this seems like it will return an incorrect type: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Good question! I would write this roughly like this (off the top of my head, so I cannot promise this is exactly right, but it's directionally correct): declare function or<T extends any[]>(...params: T): T[number];
declare module '@glint/environment-ember-loose/registry' {
export default interface Registry {
or: typeof or;
}
} Or, with a class-based helper: import type Helper from '@ember/component/helper';
declare class OrHelper<T extends any[]> extends Helper<{
Args: {
Positional: T;
};
Return: T[number];
}> {
compute(positional: T): T[number];
}
declare module '@glint/environment-ember-loose/registry' {
export default interface Registry {
or: typeof OrHelper;
}
} The use of the |
Beta Was this translation helpful? Give feedback.
-
Excellent, thank you! I verified that works! 🙇🏼 |
Beta Was this translation helpful? Give feedback.
Good question! I would write this roughly like this (off the top of my head, so I cannot promise this is exactly right, but it's directionally correct):
Or, with a class-based helper: