@types/ember__component
4.0.6 update breaks Helper.compute
without signature
#1498
-
The following code for helpers worked fine so far: import Helper from '@ember/component/helper';
export default class MyHelper extends Helper {
compute([name]: [string]) {
return name;
}
} Now throws a type error, as there is no signature defined. I guess that means I have to duplicate the type declaration in the signature for the type PropName = 'prop1' | 'prop2';
interface HelperSignature {
PositionalArgs: [obj: MyModel, propName: PropName];
}
export default class MyHelper extends Helper<HelperSignature> {
compute([obj, propName]: [MyModel, PropName]) {
// do something
}
} It becomes somewhat cumbersome to type this twice. Am I missing something here, or is this the only way to type a helper class like this going forward? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Good question! I didn't actually realize folks were using that pattern, but it makes sense. This is basically the tradeoff of making class-based helpers natively support Glint, and is an unfortunate limitation of the Typescript type system as it stands: methods whose values are related to the generic type of the class they are members of don't get inference based on the generic types. This is partly to allow subclassing to work as folks expect, and partly just because it's a hard thing TS hasn't solved yet. I've turned this into a discussion and am leaving it here so folks can share suggestions, alternative designs, etc.! One option here is: interface Args {
Positional: [obj: MyModel, propName: 'prop1' | 'prop2'];
}
export default class MyHelper extends Helper<{ Args: Args }> {
compute([obj, propName]: Args['Positional']) {
// do something
}
} That has the overhead of a little bit of repetition but is definitely much shorter. |
Beta Was this translation helpful? Give feedback.
-
Here's a possible solution: DefinitelyTyped/DefinitelyTyped#59657 |
Beta Was this translation helpful? Give feedback.
Good question! I didn't actually realize folks were using that pattern, but it makes sense. This is basically the tradeoff of making class-based helpers natively support Glint, and is an unfortunate limitation of the Typescript type system as it stands: methods whose values are related to the generic type of the class they are members of don't get inference based on the generic types. This is partly to allow subclassing to work as folks expect, and partly just because it's a hard thing TS hasn't solved yet.
I've turned this into a discussion and am leaving it here so folks can share suggestions, alternative designs, etc.!
One option here is: