gcanti / io-ts

Runtime type system for IO decoding/encoding
https://gcanti.github.io/io-ts/
MIT License
6.68k stars 331 forks source link

Add optional support to type. #668

Open colin-han opened 1 year ago

colin-han commented 1 year ago

Based on the PR #654 , I'm trying to make a more readable solution to support optional properties of TypeScript.

const TypeA = t.type({
  propA: t.optional(t.string),
  propB: t.string
})

I define a new keyword t.optional to annotate a properties is optional. The above declaration will generated type likes following:

interface TypeA {
  propA?: string;
  propB: string;
}

I borrow all UT from the PR #654 . Thanks to the author @mjburghoffer, your UT help me so many.

BTW, I don't known why I cannot pass all test (prettier and eslint) on my local environment. If it's a problem to merge this PR. let me known, I will take more research for it.

cchepelov commented 1 year ago

This looks good!

What are the blockers for integration?

colin-han commented 1 year ago

@cchepelov There still has some UT is not passed. And a newer version of TypeScript is required. I guess that is why this PR is not merged into master.

I'm sorry I have not enough time and skill to fix all issue. If the feature is required for you, you can use another library I published to NPM ( io-ts-with-optional ) as a workaround.

cchepelov commented 1 year ago

Thank you @colin-han!

I'll see if I can get away with no optionals for the time being. It looks like io-ts' 3.0 branch is moving a bit, too. Afraid I'm lacking time/skill as well to help with a fix, although I'll sure be happy if that happens

gcanti commented 1 year ago

It looks like io-ts' 3.0 branch is moving a bit, too.

@cchepelov the active development of the next major iteration of io-ts (and more generally of the fp-ts ecosystem) is happening here

colin-han commented 1 year ago

@gcanti It looks like the new Schema system is powerful. Do you have consider to support optional syntax like mine? The current optional syntax (C.struct({ a: C.string, b: C.number }, { c: C.boolean })) is a little less readable.

gcanti commented 1 year ago

@colin-han just landed in the development branch