lit / lit/lit.dev

Mixin example throws conversion error

Open
#392 3 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
142
Forks
230
PR merge metrics
No merged PRs in 30d

Description

# Description
Sorry if this is not a problem with lit itself, but rather is with typescript, I just don't know where else to put it, and it seems confusing that an official example is failing.
I was trying to implement a simple mixin for LitElement following the example here: https://lit.dev/docs/composition/mixins/
which I literally copy-pasted into a fresh project to see if it's a problem with the example itself, and it seems so.
I'm using the starter kit here:
https://github.com/PolymerLabs/lit-element-starter-ts

# Steps to Reproduce

1. Write this code into any .ts file

```ts
import {LitElement} from 'lit';
import { property } from 'lit/decorators';

type Constructor = new (...args: any[]) => T;

// Define the interface for the mixin
export declare class MyMixinInterface {
highlight: boolean;
protected renderHighlight(): unknown;
}

export const MyMixin = >(superClass: T) => {
class MyMixinClass extends superClass {
@property() highlight = false;
protected renderHighlight() {

}
};
// Cast return type to your mixin's interface intersected with the superClass type
return MyMixinClass as Constructor & T;
}
```

2. See this output as typescript typechecks some

Conversion of type '{ new (...args: any[]): MyMixinClass; prototype: MyMixin.MyMixinClass; } & T' to type 'Constructor & T' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
Type '{ new (...args: any[]): MyMixinClass; prototype: MyMixin.MyMixinClass; } & T' is not comparable to type 'Constructor'.
Type 'MyMixinClass & LitElement' is not comparable to type 'MyMixinInterface'.
Property 'renderHighlight' is protected but type 'MyMixinClass' is not a class derived from 'MyMixinInterface'.ts(2352)

## Expected Results

That there's no typescript error

## Actual Results

src/my-mixin.ts:20:9 - error TS2352: Conversion of type '{ new (...args: any[]): MyMixinClass; prototype: MyMixin.MyMixinClass; } & T' to type 'Constype: MyMixin.MyMixinClass; } & T' to type 'Constructor & T' may be a mistakeother. If this was intentional, convert the expressio because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first. mparable to type 'Constructor'.
Type '{ new (...args: any[]): MyMixinClass; prototype: MyMixin.MyMixinClass; } & T' is not comparable to type 'Constructor'. yMixinInterface'.
Type 'MyMixinClass & LitElement' is not comparable to type 'MyMixinInterface'.
Property 'renderHighlight' is protected but type 'MyMixinClass' is not a class derived from 'MyMixinInterface'.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the mixins documentation page and reproduce the copied example in the linked lit-element-starter-ts project. Run TypeScript typechecking on the shown mixin code and compare the compiler error with the example's protected method and cast. Done means the official example typechecks without the reported conversion error.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
documentation
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.