Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

unpartial

unional153.5kMIT1.0.5TypeScript support: included

Unpartial a partialed object

readme

unpartial

NPM version NPM downloads

GitHub NodeJS Codecov

Visual Studio Code Wallaby.js

Unpartial a type.

It is very common to define a config type and Partial<> it in a function argument. When we received the argument, we want to merge it with our default config before using it:

import { unpartial } from 'unpartial'

interface Config {
  require: { a: number }
  optional?: { a: number }
}
const defaultConfig: Config = { require: { a: 1 } }

function foo(givenConfig?: Partial<Config>) {
  const config = unpartial(defaultConfig, givenConfig);
  // use config with type safety
}

Code completion is available as you type:

const config = unpartial(defaultConfig, { /* code completion here */});

It also supports merging two default configs. This is useful when you are extending the interface from another package/class.

import { unpartial } from 'unpartial'

import { Option, defaultOption } from 'another-package'

interface MyOption extends Option { ... }

const myDefaultOption = { ... }

function foo(givenOption?: Partial<MyOption>) {
  const option = unpartial(defaultOption, myDefaultOption, givenOption)
}

This is deprecated because currently TypeScript does not support optional generic type, so it is not possible to create a satisfactory signature that works with both implicit and explicit type.

Instead, please use composition when combining 3 or more values:

unpartial(unpartial(defaultOption, myDefaultOption), givenOption)

There are 3 more functions available in this library:

  • unpartialRecursively(): unpartial() deeply.\ In practice, this does not seem to be useful. Maybe will be deprecated and removed in the future.
  • required(): an alternative version of unpartial() with a different type management.\ This will become identical to unpartial() in the future.
  • requiredDeep(): an alternative version of unpartialRecursively() with a different type management.\ This will become identical to unpartial() in the future.

unpartial is also exposed through type-plus. It contains many more functions and type utilities.

Feel free to check it out.

Contribute

# after fork and clone
pnpm install

# begin making changes
git checkout -b <branch>
pnpm watch

# after making change(s)
git commit -m "<commit message>"
git push

# create PR

Wallaby.js

Wallaby.js

This repository contributors are welcome to use Wallaby.js OSS License to get test results immediately as you type, and see the results in your editor right next to your code.

changelog

1.0.0

1.0.5

Patch Changes

  • cac4c5d: Improve exports/main/module fields

1.0.4

Patch Changes

  • 4e3eba3: Omit Pick when it is not needed.

1.0.3

Patch Changes

  • afa4606: Improve unpartial(base, partial) type.

    The type for unpartial(parent, base, partial) is marked as deprecated. The type is loosened now similar to required(parent, base, partial) to make it more flexible. But note that it is incorrect as the values are not deeply merged.

1.0.2

Patch Changes

  • 6070a75: Update types to fix some use cases. It is now based on the type from required() but make some improvements.

    • Result now honors | undefined property
    • Type is constructed instead of intersect, making it easier to read

1.0.1

Patch Changes

  • fa5dce0: fix cjs package.json

Major Changes

  • dabbde0: The unpartial() is rewritten to improve its type management, and ignore undefined and null properties.

    While this can be considered as a fix, it is better to release it as a breaking change this change might accidentally break consuming code.

0.7.5 (2022-06-11)

Bug Fixes

0.7.4 (2022-06-04)

Bug Fixes

0.7.3 (2022-05-31)

Bug Fixes

0.7.2 (2022-05-31)

Bug Fixes

0.7.1 (2022-05-31)

Bug Fixes

unpartial

0.7.0

Minor Changes

  • Add ESM support