-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add types for Resource and @use #30
Conversation
We definably need to have types here. But for comparison, these are the types I have been using: declare module 'ember-could-get-used-to-this' {
interface TemplateArgs {
positional?: unknown[];
named?: Record<string, unknown>;
}
type Owner = unknown;
type Thunk<T> = () => T;
declare class Resource<
Args extends TemplateArgs = TemplateArgs,
T = unknown
> {
protected readonly args!: Args;
abstract readonly value: T;
constructor(ownerOrThunk: Owner | Thunk<Args>, args?: Args): void;
setup(): void;
abstract update?(): void;
abstract teardown?(): void;
}
export declare let use: PropertyDecorator;
} |
I'll add the lifecycle methods, but:
|
TypeScript playground Demo added to description |
@NullVoxPopuli I thought interface Args {
positional: [string, string]
}
interface Value<T> {
isPending: boolean
isSettled: boolean
isRejected: boolean
isFulfilled: boolean
content: T | null
}
export default class FindDoc<T> extends Resource<Args, Value<T>> {
value: Value<T> = new TrackedObject({
isPending: false,
isSettled: false,
isRejected: false,
isFulfilled: false,
content: null,
})
... And how it's used: @use document = new FindDocument<MyModelName>(() => [
"my-model-name",
this.args.model.id,
]) This gives me the correct type hinting in the component that consumes this resource. Can we add the ability to type the value even if some people implement |
@scottmessinger In a recent PR #35 (not yet released), we changed to return the instance of the resource rather than the value. Therefore there is no need to have specific types for the value. |
@josemarluedke Just looked through that PR and I'm 💯 . For me, it feels more intuitive. I like it! |
Also adds a utility function to get the type of
value
when using@use
Demo