w3c / webvtt.js

WebVTT parser and validator
https://w3c.github.io/webvtt.js/parser.html
Creative Commons Zero v1.0 Universal
148 stars 49 forks source link

Add TypeScript types #43

Open timmywil opened 1 year ago

timmywil commented 1 year ago

I manually added types to my repo, but it's not comprehensive and only covers what I use. It would be great if this repo maintained its own types. Switching to TypeScript is not required. Only need to add a .d.ts file to the repo and set the types field in package.json.

Here's what I've gotten started, but I'm sure it's not completely accurate.

// webvtt-parser.d.ts
declare module 'webvtt-parser' {
  export interface Cue {
    direction: 'horizontal' | 'vertical'
    id: string
    startTime: number
    endTime: number
    text: string
    lineAlign: 'start' | 'center' | 'end'
    linePosition: 'auto' | number
    pauseOnExit: boolean
    positionAlign: 'auto' | 'start' | 'center' | 'end'
    size: number
    snapToLines: boolean
    textPosition: 'auto' | number
    tree: {
      children: {
        type: 'text'
        value: string
      }[]
    }
  }

  export class WebVTTParser {
    public parse: (
      input: string,
      mode: 'metadata' | 'chapters'
    ) => {
      cues: Cue[]
      errors: Error[]
      time: number
    }
  }
  export class WebVTTSerializer {
    public serialize: (cues: Cue[]) => string
  }
}

Thanks for considering.

lionel-rowe commented 8 months ago

@timmywil 's types look like a good start. It looks like Cue#tree.children has the type Child[], where Child is defined like this:

type Child = {
    type: 'text'
    value: string
} | {
    type: 'object'
    name: 'v' | 'lang'
    classes: string[]
    children: Child[]
    value: string
} | {
    type: 'object'
    name: 'c' | 'i' | 'b' | 'u' | 'ruby' | 'rt'
    classes: string[]
    children: Child[]
}

Also WebVTTParser's constructor takes entities?: Record<string, string> as a parameter.

cellulosa commented 2 months ago

any plans to implement this?