Skip to content
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

TypeScript: getByRef() error when using generic type param as Ref value #444

Closed
frank-weindel opened this issue Jan 23, 2023 · 0 comments · Fixed by #445 or #474
Closed

TypeScript: getByRef() error when using generic type param as Ref value #444

frank-weindel opened this issue Jan 23, 2023 · 0 comments · Fixed by #445 or #474
Assignees

Comments

@frank-weindel
Copy link
Contributor

frank-weindel commented Jan 23, 2023

A user on the forums asked a question about how to create a subclassable base Component that takes a generic parameter that is used to inform the type of a Ref value. In diving into a potential solution I discovered that the type definitions for getByRef() are not allowing for a genericized Ref value type, while the definitions for tag() are allowing it just fine.

Example code:

import { Lightning } from '@lightningjs/sdk'

class Header extends Lightning.Component {}

export interface IPageTemplateSpec<
  T extends Lightning.Component.Constructor = Lightning.Component.Constructor,
> extends Lightning.Component.TemplateSpec {
  Header: typeof Header
  Content: T
}

export interface IPageTypeConfig extends Lightning.Component.TypeConfig {
  IsPage: true
}

export class Page<T extends Lightning.Component.Constructor = Lightning.Component.Constructor>
  extends Lightning.Component<IPageTemplateSpec<T>, IPageTypeConfig>
  implements Lightning.Component.ImplementTemplateSpec<IPageTemplateSpec<T>>
{
  static override _template(): Lightning.Component.Template<IPageTemplateSpec> {
    return {
      w: (w: number) => w,
      h: (h: number) => h,
      rect: true,
      color: 0xff0e0e0e,

      Header: {
        type: Header,
      },
      Content: undefined,
    }
  }

  protected Content = this.getByRef('Content')!
  //                                ^-------^ Error!!!
}

The error is:

Argument of type 'string' is not assignable to parameter of type 'keyof TemplateSpecRefs<IPageTemplateSpec<T>>'.
  Type '"Content"' is not assignable to type '"Header" | (TransformPossibleElement<P, IPageTemplateSpec<T>[P], never> extends never ? never : P) | ((T extends Constructor<...> ? InstanceType<...> : Element<...>) extends never ? never : "Content")'.ts(2345)

Example Repo: https://github.com/frank-weindel/com.domain.app.LngForum546Subclassable

frank-weindel added a commit to frank-weindel/Lightning that referenced this issue Jan 23, 2023
@frank-weindel frank-weindel self-assigned this Jan 23, 2023
@uguraslan uguraslan mentioned this issue Feb 7, 2023
1 task
@uguraslan uguraslan added the bug label Mar 13, 2023
@uguraslan uguraslan added the fix-in-review This issue has a PR in review label Apr 3, 2023
@uguraslan uguraslan added this to the Lightning v2.10.0 milestone Apr 13, 2023
@uguraslan uguraslan added fixed and removed fix-in-review This issue has a PR in review labels Apr 13, 2023
@uguraslan uguraslan mentioned this issue Apr 17, 2023
2 tasks
@uguraslan uguraslan linked a pull request Apr 20, 2023 that will close this issue
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
2 participants