Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional non nullable #242

Open
DrTtnk opened this issue Sep 10, 2021 · 0 comments
Open

Optional non nullable #242

DrTtnk opened this issue Sep 10, 2021 · 0 comments

Comments

@DrTtnk
Copy link

DrTtnk commented Sep 10, 2021

I have noticed that the t.optional is equivalent to T|undefined|null which may be a quite different behaviour from T|undefined and T, in particular with the new typescript 4.4 --exactOptionalPropertyTypes

Would be possible to add to more optionals to the library? In the specific:

optionalNonNull for undefined properties, compliant with typescript without --exactOptionalPropertyTypes

// Type definition
export declare const optionalNonNull: <RT extends t.Mixed>(rt: RT, name?: string | undefined) => t.UnionC<[RT, t.UndefinedC]> & Optional;

// Implementation
export const optionalNonNull = <RT extends t.Mixed>(rt: RT, name?: string) => {
    const unionType = union([rt, undefinedType], name || rt.name + '?');
    return Object.assign(unionType, { optional: true } as Optional);
};

optionalExact for exact properties, compliant with typescript with --exactOptionalPropertyTypes

// Type definition
export declare const optionalExact: <RT extends t.Mixed>(rt: RT) => RT & Optional;

// Implementation
export const optionalExact = <RT extends t.Mixed>(rt: RT) => {
    return Object.assign(rt, { optional: true } as Optional);
};
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

No branches or pull requests

1 participant