halfnelson / nativescript-source-to-jsx-def

Walk nativescript source to generate JSX types for `svelte-type-checker-vscode`
Other
4 stars 1 forks source link

[Discussion] Should the typings provide better inference of the object in EventData? #18

Open shirakaba opened 4 years ago

shirakaba commented 4 years ago

Instead of requiring the user to assert what type the object is for the event, the typings could help out.

With typings as they are now

// ui/action-bar/action-bar.d.ts
type ActionItemAttributes = ViewBaseAttributes & {
    // ...
    onTap?: (args: EventData) => void;
    // ...
};

function onTap(args: EventData){
    const icon = (args.object as ActionItem).icon;
}

Potential improvement

// We could extend EventData (like this) or generate an identical interface.
interface NarrowedEventData <T extends Observable = Observable> extends EventData {
    object: T;
}

// ui/action-bar/action-bar.d.ts
type ActionItemAttributes<T extends ActionItem = ActionItem> = ViewBaseAttributes<T> & {
    // ...
    onTap?: (args: NarrowedEventData<T>) => void;
    // ...
};

function onTap(args: NarrowedEventData<ActionItem>){
    const icon = args.object.icon;
}

Disadvantages

Advantages