Skip to content

Commit a9fbafc

Browse files
committed
v10.0.4 🚀 - Improve optional and nullable ts types
1 parent 0013767 commit a9fbafc

5 files changed

Lines changed: 69 additions & 9 deletions

File tree

‎packages/sury/jsr.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sury/sury",
3-
"version": "10.0.3",
3+
"version": "10.0.4",
44
"license": "MIT",
55
"exclude": ["!src", "!rescript.json", "!README.md", "!package.json", "!docs"],
66
"exports": "./src/S.mjs"

‎packages/sury/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sury",
3-
"version": "10.0.3",
3+
"version": "10.0.4",
44
"private": true,
55
"description": "🧬 The fastest schema with next-gen DX",
66
"keywords": [

‎packages/sury/src/S.d.ts‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -455,25 +455,30 @@ export function tuple<Output, Input extends unknown[]>(
455455
}) => Output
456456
): Schema<Output, Input>;
457457

458-
export function optional<Output, Input, Or = undefined>(
458+
export function optional<
459+
Output,
460+
Input,
461+
Or extends Output | undefined = undefined
462+
>(
459463
schema: Schema<Output, Input>,
460464
or?: (() => Or) | Or,
461465
// To make .with work
462466
_?: never
463467
): Schema<
464-
Or extends undefined ? Output | undefined : Output | Or,
468+
Or extends undefined ? Output | undefined : Output,
465469
Input | undefined
466470
>;
467471

468-
export function nullable<Output, Input, Or = undefined>(
472+
export function nullable<
473+
Output,
474+
Input,
475+
Or extends Output | undefined = undefined
476+
>(
469477
schema: Schema<Output, Input>,
470478
or?: (() => Or) | Or,
471479
// To make .with work
472480
_?: never
473-
): Schema<
474-
Or extends undefined ? Output | undefined : Output | Or,
475-
Input | null
476-
>;
481+
): Schema<Or extends undefined ? Output | undefined : Output, Input | null>;
477482

478483
export const nullish: <Output, Input>(
479484
schema: Schema<Output, Input>

‎packages/sury/tests/S_test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,20 @@ test("Successfully parses nullable string", (t) => {
387387
expectType<TypeEqual<typeof value1, string | undefined>>(true);
388388
});
389389

390+
test("Successfully parses nullable of array with default", (t) => {
391+
const schema = S.nullable(S.array(S.string), []);
392+
const value1 = S.parseOrThrow(["foo"], schema);
393+
const value2 = S.parseOrThrow(null, schema);
394+
395+
t.deepEqual(value1, ["foo"]);
396+
t.deepEqual(value2, []);
397+
398+
expectType<TypeEqual<S.Schema<string[], string[] | null>, typeof schema>>(
399+
true
400+
);
401+
expectType<TypeEqual<typeof value1, string[]>>(true);
402+
});
403+
390404
test("Successfully parses nullable string with default", (t) => {
391405
const schema = S.nullable(S.string, "bar");
392406
const value1 = S.parseOrThrow("foo", schema);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import * as S from "sury";
2+
3+
export const databaseContextSchema = S.schema({
4+
schemas: S.nullable(
5+
S.array(
6+
S.schema({
7+
schema: S.string,
8+
tables: S.nullable(
9+
S.array(
10+
S.schema({
11+
name: S.string,
12+
columns: S.nullable(
13+
S.array(
14+
S.schema({
15+
name: S.string,
16+
type: S.string,
17+
nullable: S.boolean,
18+
default: S.nullable(S.string),
19+
})
20+
),
21+
[]
22+
),
23+
})
24+
),
25+
[]
26+
),
27+
})
28+
),
29+
[]
30+
),
31+
enums: S.nullable(
32+
S.array(
33+
S.schema({
34+
schema: S.string,
35+
name: S.string,
36+
value: S.string,
37+
})
38+
),
39+
[]
40+
),
41+
});

0 commit comments

Comments
 (0)