Skip to content

Comments

Treat struct properties that have type undefined as optional#639

Closed
thewilkybarkid wants to merge 1 commit intogcanti:masterfrom
thewilkybarkid:optional-undefined
Closed

Treat struct properties that have type undefined as optional#639
thewilkybarkid wants to merge 1 commit intogcanti:masterfrom
thewilkybarkid:optional-undefined

Conversation

@thewilkybarkid
Copy link
Contributor

This is an attempt to fix #628 (comment), by making properties in a struct that can have undefined as a value optional.

This feels quite thorny to do, and this is probably incomplete (e.g. Schemeable?).

Comment on lines +397 to +398
type Identity<T> = T
type Merge<T> = T extends any ? Identity<{ [k in keyof T]: T[k] }> : never
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +393 to +396
type UndefinedProperties<T> = {
[P in keyof T]-?: undefined extends T[P] ? P : never
}[keyof T]
type ToOptional<T> = Merge<Pick<T, Exclude<keyof T, UndefinedProperties<T>>> & Partial<Pick<T, UndefinedProperties<T>>>>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines -112 to +130
// $ExpectType Codec<unknown, { _tag: "A"; a: string; } | { _tag: "B"; b: number; }, { _tag: "A"; a: string; } | { _tag: "B"; b: number; }>
// $ExpectType Codec<unknown, { a: string; _tag: "A"; } | { b: number; _tag: "B"; }, { a: string; _tag: "A"; } | { b: number; _tag: "B"; }>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't expecting these to be reordered.

@thewilkybarkid
Copy link
Contributor Author

I'm going to close this in favour of gcanti/fp-ts#1693.

@thewilkybarkid thewilkybarkid deleted the optional-undefined branch April 6, 2022 12:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant