Effect CommunityEC
Effect Community2y ago
11 replies
spaethnl

Issue with Optional Props in TypeScript Constructor Using '@effect/schema/Schema'

With new default constructors, is it intentional that S.optional makes optional props optional in the constructor, but an S.optional with a default (S.optional({default: () => .. })) is not? See:

import * as S from '@effect/schema/Schema'
const struct = {
  optional: S.String.pipe(S.optional()),
  optionalWithDefault: S.String.pipe(S.optional({ default: () => 'default-string' })),
}
class MyClass extends S.Class<MyClass>('MyClass')(struct) {}

describe('MyClass', () => {
  it('create a MyClass instance', () => {
    // ts [2345]: Argument of type '{}' is not assignable to parameter of type '{ readonly optional?: string | undefined; readonly optionalWithDefault: string; }'.
    //   Property 'optionalWithDefault' is missing in type '{}' but required in type '{ readonly optional?: string | undefined; readonly optionalWithDefault: string; }'
    const myInstance = new MyClass({ })
  })
})

// Test result:
// test/test.test.ts > MyClass > create a MyClass instance
// Error: Struct (Type side)
// └─ ["optionalWithDefault"]
//    └─ is missing


I would expect that if S.optional translates to the constructor at all, it would translate in all cases.
Was this page helpful?