How do I type something that isn't already built in to the framework? #348
-
Per this suggestion: emberjs/rfcs#822 (comment) I was trying to figure out how to type class-based resources. after my class definition, I tried the example from the linked comment above: export interface Resource<T extends ArgsWrapper> extends InstanceType<
HelperLike<{
Args: { Named: T['named']; Positional: T['positional'] };
// this is the instance of the Resource -- which I also don't know how to get
// Return: IDontKnowHowToGetTheValueTypeOfAResourceButItGoesHere
Return: Resource<T> // how to make this work with sub classes?
}>
> {} now, I'm working with the class definition here because folks can do: class MyResource extends Resource {
@tracked foo;
}
<template>
{{#let (MyResource whateverArgs) as |instance|
{{instance.foo}}
{{/let}}
</template> In JS/TS, I have some type-help via a static method (this is for context, rather than directly related to the issue as resources are usable in both templates and js/ts): class {
instance = MyResource.from(() => whateverArgs);
} but anyway, back to
So, it seems I can remove the
static from<Instance extends Resource<ArgsWrapper>>(
this: (new (...args: unknown[]) => Instance),
context: object,
thunk?: Thunk | (() => unknown)
): Instance { 🤔 this is a lot, I made a TS playground So I guess assuming I'm using HelperLike correctly, how do I read this new error? is there anything I could do about my types to flatten it? or to make it less verbose? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
You're not 😉 What version of TS are you using? |
Beta Was this translation helpful? Give feedback.
You're not 😉
The snippet I gave you with
InstanceType
was using it correctly, and linked to an example of that exact usage working in practice in@glint/ember-environment-ember-loose
.What version of TS are you using?
InstanceType
has accepted abstract constructors since this PR that landed in 4.3. Note that writingextends InstanceType<HelperLike<...>>
in the playground you linked doesn't produce an error.