Effect CommunityEC
Effect Community2y ago
4 replies
whatplan

Runtime Error When Extending Object with Optional Keys in TypeScript

const Foo = S.Struct({
  // this one works fine
  // foo: S.String,
  foo: S.optional(S.String, { default: () => "default" }),
}).pipe(
  S.extend(
    S.Union(
      S.Struct({
        bar: S.String,
        baz: S.Undefined.pipe(S.optional()),
      }),
      S.Struct({
        baz: S.String,
        bar: S.Undefined.pipe(S.optional()),
      })
    )
  )
);


why does trying to extend a object with optional keys cause a runtime error?
1357 |           }
1358 |         }
1359 |         break;
1360 |       }
1361 |   }
1362 |   throw new Error(errors_.getSchemaExtendErrorMessage(x, y, path));
               ^
error: Unsupported schema or overlapping types
details: cannot extend (Struct (Encoded side) <-> Struct (Type side)) with { readonly bar: string; readonly baz?: undefined | undefined } | { readonly baz: string; readonly bar?: undefined | undefined }
      at node_modules/@effect/schema/dist/esm/Schema.js:1362:9
      at node_modules/effect/dist/esm/Array.js:1548:19
      at node_modules/effect/dist/esm/Array.js:1548:19
      at extendAST (node_modules/@effect/schema/dist/esm/Schema.js:1313:50)
      at node_modules/@effect/schema/dist/esm/Schema.js:1393:95
      at s.ts:7:5
Was this page helpful?