IF Commons GitHub

objects

Written to: 1 requirement: 1 proved, 0 partly, 0 failing, 0 with no test recorded. 0001-core FR-006

Objects made for exactOptionalPropertyTypes (0001-core FR-006): compact builds an object from fields some of which may be undefined, leaving those out, and its type says so exactly, so that a caller writes compact({ a, b: maybe }) instead of { a, ...(maybe === undefined ? {} : { b: maybe }) } once for each optional field.

import { compact } from "./objects.ts";
const timeout: number | undefined = undefined;
const options = compact({ url: "https://example.com", timeout }); // { url: string; timeout?: number }, and no `timeout` key

Source: commons/core/src/objects.ts

2 of 3 exports are imported by a test of the run. A test that imports an export may not call it or check what it returns.

compactfunction
function compact<T extends object>(value: T): { [K in keyof ({ [K in keyof T as K extends MaybeKeys<T> ? never : K]: T[K]; } & { [K in keyof T as K extends MaybeKeys<T> ? K : never]?: Exclude<T[K], undefined> | undefined; })]: ({ [K in keyof T as K extends MaybeKeys<T> ? never : K]: T[K]; } & { [K in keyof T as K extends MaybeKeys<T> ? K : never]?: Exclude<T[K], undefined> | undefined; })[K]; }

The object with its fields that are undefined left out (FR-064), typed exactly by Compact. Keys are kept in their order; a key named __proto__ is kept as an own field, never as the prototype.

Imported by 2 tests of the run (2 passed, 0 failed): the library's results

Compacttype
type Compact<T> = Simplify<
  { [K in keyof T as K extends MaybeKeys<T> ? never : K]: T[K] } & {
    [K in keyof T as K extends MaybeKeys<T> ? K : never]?: Exclude<T[K], undefined>;
  }
>;

T with each field whose type admits undefined made optional without undefined, and every other field kept as it is (readonly kept): the type of compact(value) under exactOptionalPropertyTypes.

Imported by 2 tests of the run (2 passed, 0 failed): the library's results

Simplifytypeno test imports it
type Simplify<T> = { [K in keyof T]: T[K] } & {};

One object type with the members of an intersection, so that it reads (and compares) as one.

Titles, headings, exports, tables, columns and requirements. Esc closes.