Comments (4)
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.
Resolved in a926884 and c3e434e.
from synectic.
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.
Merged in #1151.
from synectic.
Related Issues (20)
- `ENOENT: no such file or directory, getRoot` error thrown when branch is "prunable"
- Undo/Redo actions in Synectic editors using `redux-undo`
- `Manifest version 2 is deprecated, and support will be removed in 2023`
- Cache subscriptions drop multiple reserves on `metafiles/switchBranch` action
- Use `createAsyncThunk.withTypes<>()` from RTK 1.9 for pre-typed `createAsyncThunk`
- `FSCache` event handler uses stale `reserved` state when updating cache after edit/save
- Multithreading Redux Actions HOT 3
- Deprecate and replace `uuid` library with built-in JavaScript `Crypto` library
- Upgrade to React 18 HOT 1
- Upgrade to Yarn 4 HOT 1
- Installation via `deb` package fails on Ubuntu Linux HOT 2
- `+[CATransaction synchronize] called within transaction` on MacOS HOT 1
- Checkout or switch to remote-only branch results in `fatal: ambiguous argument '<branch>': unknown revision or path not in the working tree.` HOT 1
- `fatal: detected dubious ownership in repository` on Windows HOT 2
- `fatal: cannot lock ref 'refs/heads/extra'` on Windows HOT 1
- Switch `electron-forge` for `conveyor` packaging tool HOT 5
- Bundler updating HOT 5
- No `GitGraph` appears when selecting a repository from `Repository Map` drop-down menu HOT 4
- Remove unnecessary `RootState` typing when using `useAppSelector`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from synectic.