Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some type definitions [Typescript] #137

Open
lelukas opened this issue Sep 15, 2022 · 1 comment
Open

Some type definitions [Typescript] #137

lelukas opened this issue Sep 15, 2022 · 1 comment

Comments

@lelukas
Copy link

lelukas commented Sep 15, 2022

I created types for the parse function. I'm only using this one in my application. If it helps, here it goes.

If you're new to Typescript:
Create a .d.ts file inside a folder and reference that folder in your tsconfig.json file by editing the typeRoots array.
Example:

"typeRoots": ["src/customTypings"],

while having src > customTypings > himalaya.d.ts

declare module 'himalaya' {
  export function parse(str: string, options?: any): Array<AnyElement>

  export type Type = "element" | "comment" | "text"

  export interface Node {
    type: Type;
  }

  export interface Attribute {
    key: string;
    value: string | null;
  }

  export interface Element extends Node {
    type: "element";
    tagName: string;
    children: AnyElement[];
    attributes: Attribute[];
  }

  export interface Comment extends Node {
    type: "comment";
    content: string;
  }

  export interface Text extends Node {
    type: "text";
    content: string;
  }

  export type AnyElement = Element | Comment | Text
}
@lelukas lelukas changed the title Some type definitions Some type definitions [Typescript] Sep 15, 2022
@mparpaillon
Copy link

Very useful thanks.
I would probably deduce the type from the nodes though. Something like

declare module 'himalaya' {
  export interface Attribute {
    key: string;
    value: string | null;
  }

  export interface Element {
    type: "element";
    tagName: string;
    children: Node[];
    attributes: Attribute[];
  }

  export interface Comment {
    type: "comment";
    content: string;
  }

  export interface Text {
    type: "text";
    content: string;
  }

  export type Node = Element | Comment | Text;

  export function parse(str: string, options?: any): Node[]
  export function stringify(elements: Node[]): string
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants