Skip to content

option to generate TypeScript interface rather than type #8

@nilslice

Description

@nilslice

I am finding that it makes for a nicer workflow when I use interfaces rather than types to model data coming from a JSON xhr endpoint. My use case is closely aligned with Ponzu, for which I understand could be too specific to alter typewriter.

Here is my typical layout:

export interface ItemData {
    uuid: string;
    id: number;
    slug: string;
    timestamp: number;
    updated: number;
}

export interface VideoData extends ItemData {
    title: string;
    category: string;
    duration: number;
}

export class Item implements ItemData {
    uuid: string;
    id: number;
    timestamp: number;
    updated: number;
    slug: string;

    constructor(data: ItemData) {
        this.uuid = data.uuid;
        this.id = data.id;
        this.timestamp = data.timestamp;
        this.updated = data.updated;
        this.slug = data.slug;
    }

    // ... methods for inheritance by other classes
}

export default class Video extends Item {
    title: string;
    category: string;
    duration: number;

    constructor(data: VideoData) {
        super(data);
 
        this.title = data.title;
        this.category = data.category;
        this.duration = data.duration;
     }
}

I do this simply because having a class extend an Item seems to be the only way to get the class to inherit the values and methods.. If Item were a type, I don't see how I can gain the same level of inheritance (of properties or methods) between my classes that I do by using something like Item as a base class.

Again, I know this is unique to Ponzu and the situation where classes are tied directly to data coming in as JSON from a xhr call so it might be too specific. If you have any suggestions as to how I might be able to accomplish the same outcome as above by using the types generated by Typewriter, I would be happy to know!

The purpose of this issue is to see if there is a reason to extend Typewriter with a "generate interfaces" option, or if there is a better way for me to be structuring my client code.

Thanks for making Typewriter -- I've shared it with a number of folks asking about making web clients for Ponzu.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions