Optional props with exactOptionalPropertyTypes=true
How do you handle the case, when you have a 3rdparty type with many optional nonnullable props?
Otherwise a Schema can be written and use Option, which makes all props mandatory
Maybe you have other idea?
What do you think?
// 3rdparty type, cannot be changed
interface Foobar {
foo: number,
// due to exactOptionalPropertyTypes=true, undefined cannot be assigned
bar?: string
// there can be a lots of other optional props here
}
declare const input: Foobar;
const optional = 'foo' as string | undefined;
// This works, but what if there are many more optional types
const result = optional ? { ...input, bar: optional } : input;
// Maybe a new helper for Struct???
const result1 = Struct.setOptional(input, 'bar', optional);
// With pipe many optional props can be handled properly
const result2 = pipe(input, Struct.setOptional('bar', optional));// 3rdparty type, cannot be changed
interface Foobar {
foo: number,
// due to exactOptionalPropertyTypes=true, undefined cannot be assigned
bar?: string
// there can be a lots of other optional props here
}
declare const input: Foobar;
const optional = 'foo' as string | undefined;
// This works, but what if there are many more optional types
const result = optional ? { ...input, bar: optional } : input;
// Maybe a new helper for Struct???
const result1 = Struct.setOptional(input, 'bar', optional);
// With pipe many optional props can be handled properly
const result2 = pipe(input, Struct.setOptional('bar', optional));Otherwise a Schema can be written and use Option, which makes all props mandatory
const FooBarSchema = Schema.Struct({
foo: Schema.Number,
bar: Schema.optional(Schema.String, { exact: true, as: 'Option' })
});
// { foo: number; bar?: string; }
type Encoded = Schema.Schema.Encoded<typeof FooBarSchema>
// { foo: number; bar: Option<string>; }
type Decoded = Schema.Schema.Type<typeof FooBarSchema>const FooBarSchema = Schema.Struct({
foo: Schema.Number,
bar: Schema.optional(Schema.String, { exact: true, as: 'Option' })
});
// { foo: number; bar?: string; }
type Encoded = Schema.Schema.Encoded<typeof FooBarSchema>
// { foo: number; bar: Option<string>; }
type Decoded = Schema.Schema.Type<typeof FooBarSchema>Maybe you have other idea?
What do you think?
