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

Nullable type #1045

Open
1 task done
Ghost-str opened this issue Jan 17, 2025 · 1 comment
Open
1 task done

Nullable type #1045

Ghost-str opened this issue Jan 17, 2025 · 1 comment

Comments

@Ghost-str
Copy link
Contributor

Ghost-str commented Jan 17, 2025

Type description + examples

Set null for each property of object
It can be useful in React because the framework determines the component type by value (controlled and uncontrolled).

type SomeEntity = {
 foo: string;
 bar: string;
};

type FormState = Nullable<SomeEntity>;

const SomeInitialState: FormState = {
 foo: null,
 bar: null
} 

Type source

type Nullable<T extends object> = { [K in keyof T]: T[K] | null };

Search existing types and issues first

  • I tried my best to look for it
Repository owner deleted a comment Jan 18, 2025
@fregante
Copy link
Collaborator

"Nullable" sounds like the opposite of NonNullable, which drops null | undefined from X | null | undefined https://www.typescriptlang.org/docs/handbook/utility-types.html#nonnullabletype

What you're suggesting sounds like Partial, except it accepts null: https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype

But… sindresorhus/meta#7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants