New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@anywidget/types

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@anywidget/types - npm Package Compare versions

Comparing version

to
0.1.2

index.test.ts

19

CHANGELOG.md
# @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 @@

22

index.d.ts
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"
}
}