Git Product home page Git Product logo

Comments (4)

nelsonni avatar nelsonni commented on June 26, 2024

The NonUndefinedProperties<T> utility type can be further refined to correctly remove never values from the subsequent type by using a combination of Mapped Types and Conditional Types to create a new utility type:

type NonNullableProperties<T> = { [P in keyof T as T[P] extends undefined | null ? never : P]: T[P] };

However, this brings up an additional question about the behavior differences of NonUndefinedProperties<T> and NonNullableProperties<T>; since they fundamentally do different things. NonUndefinedProperties<T> narrows and refines property types to remove nullable subtypes (i.e. undefined | null | void).NonNullableProperties<T> refines the set of properties to remove any nullable properties (i.e. properties whose type is entirely composed of nullable subtypes).

As an illustrative example, see below:

type T =  {
  a: number;
  b: string | undefined;
  c: undefined;
  d: boolean | null
};

type NonUndefined = NonUndefinedProperties<T>;
// type NonUndefined = Record<string, never> | {
//   a: number;
//   b: string;
//   c: never;
//   d: boolean;
// }

type NonNullable = NonNullableProperties<T>;
// type NonNullable = {
//   a: number;
//   b: string | undefined;
//   d: boolean | null;
// }

Both typings are useful for different purposes, so it might be better to keep both types and update the utils.removeUndefinedProperties function to handle both type refinements.

from synectic.

nelsonni avatar nelsonni commented on June 26, 2024

Resolved in a926884 and c3e434e.

from synectic.

nelsonni avatar nelsonni commented on June 26, 2024

The finalized types are:

export type NonNullableProperties<T> = { 
   [P in keyof T as T[P] extends undefined | null | void ? never : P]: T[P] 
};

 type NonNullableObject<T> = {
  [P in keyof NonNullableProperties<T>]-?: Exclude<T[P], null | undefined | void>;
} | Record<string, never>;

Which allows for a properly typed utility function:

export const removeNullableProperties = <V, T extends Record<string | number | symbol, V>>(obj: T)
: NonNullableObject<T> => {
  return Object.entries(obj)
    .filter((e): e is [string, NonNullable<V>] => e[1] !== undefined && e[1] !== null)
    .reduce((acc, [k, v]) => ({ ...acc, [k]: v }), {});
};

from synectic.

nelsonni avatar nelsonni commented on June 26, 2024

Merged in #1151.

from synectic.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.