Socket
Book a DemoInstallSign in
Socket

ts-undefined-partial

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-undefined-partial

TypeScript recursive conversion between optional (partial) and undefined properties.

1.0.2
latest
Source
npmnpm
Version published
Weekly downloads
24K
-37.87%
Maintainers
1
Weekly downloads
 
Created
Source

ts-undefined-partial: TypeScript recursive conversion between optional (partial) and undefined properties

The library has 2 typing utilities:

  • PartialToUndefined<T> removes "?" optionality marker on all properties (recursively) and replaces them with | undefined. I.e. it makes the type "stricter" in assignments.
  • UndefinedToPartial<T> adds "?" optionality marker for all properties (recursively) which can accept undefined as a value. I.e. it makes the type "more relaxed" in assignments.

These tools ignore "complex" objects which have at lease 1 method on them (like Date, Map etc.). I.e. the library is suited for data objects only.

Examples

PartialToUndefined<{ a?: string; c: MyClass; some: { x?: string } }>
// -> { a: string | undefined; c: MyClass; some: { x: string | undefined } }

UndefinedToPartial<{ a: string | undefined; c: MyClass; some: { x: string | undefined } }>
// -> { a?: string | undefined; c: MyClass; some: { x?: string | undefined } }

Background

For object properties, TypeScript supports two slightly different notions of "optionality":

  • whether a property is "required" or "optional" ("?" suffix marker);
  • whether a property accepts undefined as a value or not.

Examples:

let optional: {
  a?: number;
};
optional = {}; // OK; property can be omitted
optional = { a: undefined }; // OK

let undefinable: {
  a: number | undefined;
};
optional = {}; // ERROR
optional = { a: undefined }; // OK

There is also the 3rd notion (a?: number | undefined) which is technically different, but in practice, TypeScript can't distinguish it from a?: number in many cases, especially when working with generics.

Keywords

typescript

FAQs

Package last updated on 11 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.