gcanti / io-ts-codegen

Code generation for io-ts
https://gcanti.github.io/io-ts-codegen/
MIT License
156 stars 14 forks source link

Typescript reports cannot find name 'DateFromISOString' for type generated with io-ts-codegen #43

Closed SAnDAnGE closed 5 years ago

SAnDAnGE commented 5 years ago

In my io-gen ts file:

import *  as gen from 'io-ts-codegen';

const declarations = [
  gen.typeDeclaration(
    'Category',
    gen.typeCombinator(
      [
        gen.property('id', gen.identifier('UUID')),
        gen.property('added_at', gen.identifier('DateFromISOString')), // FIXME: shows typescript error in generated code
      ],
      'Category'
    ),
    true
  )
];

const sorted = gen.sort(declarations);
export const generatedCode = [
  "import * as t from 'io-ts';",
  "import { UUID } from 'io-ts-types/lib/UUID';",
  "import { DateFromISOString } from 'io-ts-types/lib/Date/DateFromISOString';\n",
  ...sorted.map(d => gen.printStatic(d)),
  ...sorted.map(d => gen.printRuntime(d))
].join('\n');

generates the following code:

import * as t from 'io-ts';
import { UUID } from 'io-ts-types/lib/UUID';
import { DateFromISOString } from 'io-ts-types/lib/Date/DateFromISOString';

export interface Category {
  id: UUID,
  added_at: DateFromISOString
}
export const Category = t.type({
  id: UUID,
  added_at: DateFromISOString
}, 'Category')

I get this error int the Category interface Cannot find name 'DateFromISOString'.

I made a stackblitz project to show this error and explain it better:

https://stackblitz.com/edit/typescript-qjo4o3

Thank you for your work ! 🥇

gcanti commented 5 years ago

@SAnDAnGE I've updated the PR (and the 0.3.4-lib branch) with a possible fix, could you please try it out by running npm i gcanti/io-ts-codegen#0.3.4-lib?

SAnDAnGE commented 5 years ago

Hello gcanti, thank you very much! Currently i can not test it because i'm on holiday for 1 week, but as soon as I get back, I will let you know.

On Wed, Jul 10, 2019, 10:18 Giulio Canti notifications@github.com wrote:

@SAnDAnGE https://github.com/SAnDAnGE I've updated the PR (and the 0.3.4-lib branch) with a possible fix, could you please try it out by running npm i gcanti/io-ts-codegen#0.3.4-lib?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/gcanti/io-ts-codegen/issues/43?email_source=notifications&email_token=AATZ5HPLA2YJYD7AH6K6OSTP6WELBA5CNFSM4H7LZUF2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZSRLZQ#issuecomment-509941222, or mute the thread https://github.com/notifications/unsubscribe-auth/AATZ5HI3EPNHA2LGVLB4L3LP6WELBANCNFSM4H7LZUFQ .

SAnDAnGE commented 5 years ago

Seems to work 👍 Thank you !