-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ok finally both refs and selected properties for template type checki…
…ng working as expected, by intersecting the custom element class with the DefineComponent type. Ugly, but it works.
- Loading branch information
Showing
2 changed files
with
28 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,29 @@ | ||
import type {DefineComponent} from 'vue' | ||
|
||
// Select the properties to expose to template type checking. | ||
type KitchenSinkAttributes = 'foo' | 'bar' | ||
|
||
export class KitchenSink extends HTMLElement { | ||
foo = 123 | ||
bar = false | ||
|
||
// This is not exposed to template type checking. | ||
someMethod() { | ||
/* ... */ | ||
} | ||
} | ||
|
||
customElements.define('kitchen-sink', KitchenSink) | ||
|
||
import type {Component} from 'vue' | ||
type VueElementAttribtues<T extends HTMLElement, Attributes extends keyof T> = | ||
// This is the type that is exposed to template type checking. | ||
DefineComponent<Partial<Pick<T, Attributes>>> & | ||
// Additionally intersect with a constructor type that returns the element type, for refs to work properly. | ||
(new () => T) | ||
|
||
declare module 'vue' { | ||
interface GlobalComponents { | ||
// Try toggling between these two lines | ||
'kitchen-sink': typeof KitchenSink // not working (except ref works) | ||
// 'kitchen-sink': Component<{foo: number}> // works (except ref not working) | ||
// Use the helper to add any custom element to GlobalComponents. | ||
'kitchen-sink': VueElementAttribtues<KitchenSink, KitchenSinkAttributes> | ||
} | ||
} |