Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions packages/types/src/transform/no-undefined.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Client } from "../client";
import { CommandIO } from "../command";
import type { HttpHandlerOptions } from "../http";
import type { MetadataBearer } from "../response";
import { DocumentType } from "../shapes";
import type { Exact } from "./exact";
import type { AssertiveClient, NoUndefined, UncheckedClient } from "./no-undefined";

Expand All @@ -13,6 +14,7 @@ type A = {
required: string | undefined;
optional?: string;
nested: A;
document: DocumentType;
};

{
Expand All @@ -22,6 +24,8 @@ type A = {
const assert1: Exact<T["required"], string> = true as const;
const assert2: Exact<T["nested"]["required"], string> = true as const;
const assert3: Exact<T["nested"]["nested"]["required"], string> = true as const;
const assert4: Exact<T["document"], DocumentType> = true as const;
const assert5: Exact<T["nested"]["document"], DocumentType> = true as const;
}

{
Expand All @@ -30,13 +34,15 @@ type A = {
b: number | undefined;
c: string | number | undefined;
optional?: string;
document: DocumentType | undefined;
};

type MyOutput = {
a?: string;
b?: number;
c?: string | number;
r?: MyOutput;
document?: DocumentType;
} & MetadataBearer;

type MyConfig = {
Expand Down Expand Up @@ -66,17 +72,20 @@ type A = {
a: "",
b: 0,
c: 0,
document: { aa: "b" },
};
const get = c.getObject(input);
const output = null as unknown as Awaited<typeof get>;

const assert1: Exact<typeof output.a, string | undefined> = true as const;
const assert2: Exact<typeof output.b, number | undefined> = true as const;
const assert3: Exact<typeof output.c, string | number | undefined> = true as const;
const assert4: Exact<typeof output.document, DocumentType | undefined> = true as const;
if (output.r) {
const assert4: Exact<typeof output.r.a, string | undefined> = true as const;
const assert5: Exact<typeof output.r.b, number | undefined> = true as const;
const assert6: Exact<typeof output.r.c, string | number | undefined> = true as const;
const assert5: Exact<typeof output.r.a, string | undefined> = true as const;
const assert6: Exact<typeof output.r.b, number | undefined> = true as const;
const assert7: Exact<typeof output.r.c, string | number | undefined> = true as const;
const assert8: Exact<typeof output.r.document, DocumentType | undefined> = true as const;
}
}

Expand All @@ -88,16 +97,19 @@ type A = {
a: "",
b: 0,
c: 0,
document: { aa: "b" },
};
const get = c.getObject(input);
const output = null as unknown as Awaited<typeof get>;

const assert1: Exact<typeof output.a, string> = true as const;
const assert2: Exact<typeof output.b, number> = true as const;
const assert3: Exact<typeof output.c, string | number> = true as const;
const assert4: Exact<typeof output.r.a, string> = true as const;
const assert5: Exact<typeof output.r.b, number> = true as const;
const assert6: Exact<typeof output.r.c, string | number> = true as const;
const assert4: Exact<typeof output.document, DocumentType> = true as const;
const assert5: Exact<typeof output.r.a, string> = true as const;
const assert6: Exact<typeof output.r.b, number> = true as const;
const assert7: Exact<typeof output.r.c, string | number> = true as const;
const assert8: Exact<typeof output.r.document, DocumentType> = true as const;
}

{
Expand All @@ -109,10 +121,12 @@ type A = {
const assert1: Exact<typeof output.a, string | undefined> = true as const;
const assert2: Exact<typeof output.b, number | undefined> = true as const;
const assert3: Exact<typeof output.c, string | number | undefined> = true as const;
const assert4: Exact<typeof output.document, DocumentType | undefined> = true as const;
if (output.r) {
const assert4: Exact<typeof output.r.a, string | undefined> = true as const;
const assert5: Exact<typeof output.r.b, number | undefined> = true as const;
const assert6: Exact<typeof output.r.c, string | number | undefined> = true as const;
const assert5: Exact<typeof output.r.a, string | undefined> = true as const;
const assert6: Exact<typeof output.r.b, number | undefined> = true as const;
const assert7: Exact<typeof output.r.c, string | number | undefined> = true as const;
const assert8: Exact<typeof output.r.document, DocumentType | undefined> = true as const;
}
}

Expand All @@ -125,10 +139,12 @@ type A = {
const assert1: Exact<typeof output.a, string | undefined> = true as const;
const assert2: Exact<typeof output.b, number | undefined> = true as const;
const assert3: Exact<typeof output.c, string | number | undefined> = true as const;
const assert4: Exact<typeof output.document, DocumentType | undefined> = true as const;
if (output.r) {
const assert4: Exact<typeof output.r.a, string | undefined> = true as const;
const assert5: Exact<typeof output.r.b, number | undefined> = true as const;
const assert6: Exact<typeof output.r.c, string | number | undefined> = true as const;
const assert5: Exact<typeof output.r.a, string | undefined> = true as const;
const assert6: Exact<typeof output.r.b, number | undefined> = true as const;
const assert7: Exact<typeof output.r.c, string | number | undefined> = true as const;
const assert8: Exact<typeof output.r.document, DocumentType | undefined> = true as const;
}
}
}
7 changes: 6 additions & 1 deletion packages/types/src/transform/no-undefined.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { InvokeMethod, InvokeMethodOptionalArgs } from "../client";
import type { GetOutputType } from "../command";
import type { DocumentType } from "../shapes"

/**
* @public
Expand Down Expand Up @@ -33,7 +34,9 @@ export type UncheckedClient<Client extends object> = UncheckedClientOutputTypes<
* Excludes undefined recursively.
*/
export type NoUndefined<T> = T extends Function
? T
? T
: T extends DocumentType
? T
: [T] extends [object]
? {
[key in keyof T]: NoUndefined<T[key]>;
Expand All @@ -46,6 +49,8 @@ export type NoUndefined<T> = T extends Function
* Excludes undefined and optional recursively.
*/
export type RecursiveRequired<T> = T extends Function
? T
: T extends DocumentType
? T
: [T] extends [object]
? {
Expand Down
Loading