replace-nullable-with-optional
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

replace-nullable-with-optional

TS type utils for replacing object's nullable fields with optional.

Install

npm install replace-nullable-with-optional
yarn add replace-nullable-with-optional

Types Example

import type { ReplaceNullableWithOptional, ReplaceOptionalWithNullable } from 'replace-nullable-with-optional';

type ExampleNullable = {
    a: string,
    b: number | null,
}

const exampleNullableReplaced: ReplaceNullableWithOptional<ExampleNullable> = {
  a: 'test',
  b: undefined,
}

type ExampleOptional = {
  a: string,
  b?: number,
}

const exampleOptionalReplaced: ReplaceOptionalWithNullable<ExampleOptional> = {
  a: 'test',
  b: null,
}

Utils Example

import { replaceNullableWithUndefined, replaceOptionalWithNull } from 'replace-nullable-with-optional';

const testObjectWithNullable = {
  a: 'test',
  b: null,
  c: undefined,
};

const resultWithReplacedNullable = replaceNullableWithUndefined(testObjectWithNullable);
expect(resultWithReplacedNullable).toEqual({
  a: 'test',
});

const testObjectWithOptional = {
  a: 'test',
  b: 1,
  c: null,
  d: undefined,
};

const resultWithReplacedOptional = replaceOptionalWithNull(testObjectWithOptional);
expect(resultWithReplacedOptional).toEqual({
  ...testObjectWithOptional,
  d: null,
});

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.0.00latest

Version History

VersionDownloads (Last 7 Days)Published
1.0.00

Package Sidebar

Install

npm i replace-nullable-with-optional

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

3.65 kB

Total Files

4

Last publish

Collaborators

  • magistrser