Skip to content
This repository was archived by the owner on Apr 25, 2023. It is now read-only.
This repository was archived by the owner on Apr 25, 2023. It is now read-only.

Properly type ngOnSetup method return #2

Open
@crutchcorn

Description

@crutchcorn

I would love for the ngOnSetup function to enforce the return type of the setup function.

While I would love for the typings to be as simple as:

  ngOnSetup(props: this): Partial<Record<keyof this, any>>

I seem to get errors while doing so:

TS2322: Type '{ test: Ref ; addToPlus: () => void; }' is not assignable to type 'Partial  >'.

What's interesting about this error in particular is that Partial is not a primitive type, but rather a utility type. This makes me think there is a bug in TypeScript


Playing around with the typings, I managed to get this working on the consuming side of things:

  ngOnSetup(props: this): Partial<Record<keyof InstanceType<typeof AppComponent>, any>> {
    const test = ref(12);

    const addToPlus = (): void => {
      test.value += 1;
    };

    return {
      test,
      addToPlus
    };
  }

However, while trying to extract this logic out to a sensible version in the library itself, I saw errors. The following is the closest approximation to the types that I wanted to see:

// lib/Setup.ts
export type SetupReturn<T extends new (...args: any) => any> = Partial<Record<keyof T, any>>;

export declare interface OnSetup {
  ngOnSetup<T>(props: this): SetupReturn<T extends new (...args: any) => any>;
}
// app/app.component.ts

ngOnSetup(props: this): SetupReturn<InstanceType<typeof AppComponent>> {
  const test = ref(12);

  const addToPlus = (): void => {
    test.value += 1;
  };

  return {
    test,
    addToPlus
  };
}

However, this code doesn't work either. I get the following error:

TS2416: Property 'ngOnSetup' in type 'AppComponent' is not assignable to the same property in base type 'OnSetup'.   Type '(props: this) => Partial<Record<"prototype", any>>' is not assignable to type '<T>(props: this) => Partial<Record<keyof (T extends new (...args: any) => any ? any : any), any>>'.     Type 'Partial<Record<"prototype", any>>' is not assignable to type 'Partial<Record<keyof (T extends new (...args: any) => any ? any : any), any>>'

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions