oy icon indicating copy to clipboard operation
oy copied to clipboard

Typescript Definition Files

Open Savij opened this issue 9 years ago • 4 comments
trafficstars

Hi would be great to have .d.ts files for this project. Would you happen to already have that and be able to share?

Thanks, -Jeff

Savij avatar Mar 28 '16 16:03 Savij

There isn't a TypeScript definition file made for Oy yet. It will likely take me some time to read up on creating and verifying one (and can't promise a date), so I'm happy to accept pull requests in the meantime. I'll leave this bug open for tracking purposes.

revivek avatar Mar 29 '16 01:03 revivek

Something like the below is a pretty rough but effective enough beginning, for anyone looking at types for Oy.

it lets you import { Table } from 'oy-vey' as well as

import oy from 'oy-vey'
const { Table, renderTemplate} = Oy

but it doesn't let you import { renderTemplate } from'oy-vey, as allowing this has resulted in runtime exceptions for me, so I presume that's how the package is structured.

The types:

declare module 'oy-vey' {
  export const Table: React.DetailedHTMLFactory<Oy.OyTableElements, HTMLTableElement>;
  export const TBody: React.DetailedHTMLFactory<Oy.OyTBodyElements, HTMLTableSectionElement>;
  export const TR: React.DetailedHTMLFactory<Oy.OyTRElements, HTMLTableRowElement>;
  export const TD: React.DetailedHTMLFactory<Oy.OyTDElements, HTMLTableDataCellElement>;
  export const Img: React.DetailedHTMLFactory<Oy.OyImgElements, HTMLImageElement>;
  export const A: React.DetailedHTMLFactory<Oy.OyAElements, HTMLAnchorElement>;
  const renderTemplate: (jsx: JSX.Element, opts: Oy.RenderOpts) => string;

  export default { Table, TBody, TR, TD, Img, A, renderTemplate };
}

declare namespace Oy {
  interface OyElement {
    width?: number | string;
    height?: number | string;
    align?: string;
    background?: string;
    bgColor?: string;
    border?: number | string;
    vAlign?: string;
  }

  interface OyTBodyElements extends React.HTMLAttributes<HTMLTableSectionElement>, OyElement {}
  interface OyTableElements extends React.TableHTMLAttributes<HTMLTableElement>, OyElement {}
  interface OyTRElements extends React.HTMLAttributes<HTMLTableRowElement>, OyElement {}
  interface OyTDElements extends React.TdHTMLAttributes<HTMLTableDataCellElement>, OyElement {}
  interface OyImgElements extends React.ImgHTMLAttributes<HTMLImageElement>, OyElement {}
  interface OyAElements extends React.AnchorHTMLAttributes<HTMLAnchorElement>, OyElement {}

  export type RenderOpts = {
    title: string;
    previewText: string;
    headCSS?: string;
    bgColor?: string;
    lang?: string;
    dir?: string;
  };
}

---EDIT--- I've actually improved everything I didn't like about the types since posting them, so I'll probably make a PR to DefinitelyTyped with these in the near future.

sampsonjoliver avatar Oct 05 '17 03:10 sampsonjoliver

@sampsonjoliver you should make the PR to this repo instead, if @revivek will accept it. Always better to bundle the types with the project when possible.

ianjsikes avatar Nov 22 '17 18:11 ianjsikes

Whoops, totally forgot to push my types and make the PR on DefinitelTyped in the first place.

@ianjsikes I agree, bundled types are aces. The types are totally comprehensive though, so I'd advocate giving a thorough checking-over to make sure nothing's missing, before introducing strict-type checking errors to user's projects.

Anyhoo, here's a PR @revivek https://github.com/revivek/oy/pull/82

sampsonjoliver avatar Nov 23 '17 05:11 sampsonjoliver