gristlabs / ts-interface-builder

Compile TypeScript interfaces into a description that allows runtime validation
Apache License 2.0
132 stars 28 forks source link

Enum #6

Closed Bissector closed 5 years ago

Bissector commented 5 years ago

Thank you for adding support for enums! Please take a look at the issue below.

package.json:

"dependencies": {
    "ts-interface-checker": "^0.1.5"
  },
  "devDependencies": {
    "ts-interface-builder": "^0.1.6"
  }

TS source:

export interface ILimit {
  amount: number | null;
  period: Period | null;
}

export enum Period {
  MONTH = "MONTH",
  WEEK = "WEEK",
}

builder results:

/**
 * This module was automatically generated by `ts-interface-builder`
 */
import * as t from "ts-interface-checker";
// tslint:disable:object-literal-key-quotes

export const ILimit = t.iface([], {
  "amount": t.union("number", "null"),
  "period": t.union("Period", "null"),
});

export const Period = t.enumtype({
  "MONTH": "MONTH",
  "WEEK": "WEEK",
});

const exportedTypeSuite: t.ITypeSuite = {
  ILimit,
  Period,
};
export default exportedTypeSuite;

TS compiler errors:

schema-validation.ts:12:25 - error TS2339: Property 'enumtype' does not exist on type 'typeof import("/node_modules/ts-interface-checker/dist/index")'.

12 export const Period = t.enumtype({
                           ~~~~~~~~
dsagal commented 5 years ago

Apologies! Fixed and pushed as ts-interface-checker 0.1.6.

Bissector commented 5 years ago

Thanks! I did not put it to test in term of real runtime checking, but generated file is compiled without errors now. Cheers