@anywidget/types
Advanced tools
Comparing version
# @anywidget/types | ||
## 0.1.2 | ||
### Patch Changes | ||
- feat: Autocomplete event names for known model events ([#151](https://github.com/manzt/anywidget/pull/151)) | ||
```javascript | ||
/** | ||
* @typedef Model | ||
* @prop {number} value - the current count | ||
*/ | ||
/** @type {import("@anywidget/types").Render<Model>} */ | ||
export function render({ model, el }) { | ||
model.on("change:value", () => { /* ... */); | ||
// ^ auto-completed in editor | ||
} | ||
``` | ||
## 0.1.1 | ||
@@ -4,0 +23,0 @@ |
type MaybePromise<T> = T | Promise<T>; | ||
type ObjectHash = Record<string, any>; | ||
@@ -13,7 +12,18 @@ type CleanupFn = () => MaybePromise<void>; | ||
/** | ||
* Utility type to infer possible event names from a model. | ||
* | ||
* Autocomplete works for literal string unions, but adding a union | ||
* of `string` negates autocomplete entirely. This is a workaround | ||
* to provide autocomplete but still allow any string. | ||
* | ||
* @see https://github.com/microsoft/TypeScript/issues/29729 | ||
*/ | ||
type EventName<T extends PropertyKey> = `change:${T & string}` | 'msg:custom' | (string & {}); | ||
export interface AnyModel<T extends ObjectHash = ObjectHash> { | ||
get<K extends keyof T>(key: K): T[K]; | ||
set<K extends keyof T>(key: K, value: T[K]): void; | ||
off(eventName?: string | null, callback?: EventHandler | null): void; | ||
on(eventName: string, callback: EventHandler): void; | ||
off<K extends keyof T>(eventName?: EventName<K> | null, callback?: EventHandler | null): void; | ||
on<K extends keyof T>(eventName: EventName<K>, callback: EventHandler): void; | ||
save_changes(): void; | ||
@@ -23,4 +33,4 @@ send(content: any, callbacks?: any, buffers?: ArrayBuffer[] | ArrayBufferView[]): void; | ||
export interface RenderContext<Model extends AnyModel> { | ||
model: Model; | ||
export interface RenderContext<T extends ObjectHash = ObjectHash> { | ||
model: AnyModel<T>; | ||
el: HTMLElement; | ||
@@ -30,3 +40,3 @@ } | ||
export interface Render<T extends ObjectHash = ObjectHash> { | ||
(context: RenderContext<AnyModel<T>>): MaybePromise<void | CleanupFn>; | ||
(context: RenderContext<T>): MaybePromise<void | CleanupFn>; | ||
} |
{ | ||
"name": "@anywidget/types", | ||
"type": "module", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "utility types for anywidget", | ||
@@ -16,4 +16,4 @@ "main": "index.js", | ||
"scripts": { | ||
"check": "tsc --noEmit && publint" | ||
"typecheck": "tsc --noEmit" | ||
} | ||
} |
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
5746
89.39%9
50%75
212.5%0
-100%36
Infinity%