Skip to content

Commit ee94fbe

Browse files
committed
export easily nameable constraint
1 parent 4d8e2a9 commit ee94fbe

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

generated/lib.dom.d.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34084,11 +34084,7 @@ declare function setTimeout(
3408434084

3408534085
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/structuredClone) */
3408634086
declare function structuredClone<
34087-
const T extends BetterTypeScriptLibInternals.StructuredClone.NeverOrUnknown<
34088-
BetterTypeScriptLibInternals.StructuredClone.StructuredCloneOutput<
34089-
BetterTypeScriptLibInternals.StructuredClone.AvoidCyclicConstraint<T>
34090-
>
34091-
>,
34087+
const T extends BetterTypeScriptLibInternals.StructuredClone.Constraint<T>,
3409234088
>(
3409334089
value: T,
3409434090
options?: StructuredSerializeOptions,
@@ -34863,7 +34859,13 @@ declare namespace BetterTypeScriptLibInternals {
3486334859

3486434860
type AvoidCyclicConstraint<T> = [T] extends [infer R] ? R : never;
3486534861

34866-
// 上限が不正にきつくなっているのを無視する
3486734862
type NeverOrUnknown<T> = [T] extends [never] ? never : unknown;
34863+
34864+
export type Constraint<T> =
34865+
BetterTypeScriptLibInternals.StructuredClone.NeverOrUnknown<
34866+
BetterTypeScriptLibInternals.StructuredClone.StructuredCloneOutput<
34867+
BetterTypeScriptLibInternals.StructuredClone.AvoidCyclicConstraint<T>
34868+
>
34869+
>;
3486834870
}
3486934871
}

lib/lib.dom.d.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,18 +176,20 @@ declare namespace BetterTypeScriptLibInternals {
176176

177177
type AvoidCyclicConstraint<T> = [T] extends [infer R] ? R : never;
178178

179-
// 上限が不正にきつくなっているのを無視する
180179
type NeverOrUnknown<T> = [T] extends [never] ? never : unknown;
180+
181+
export type Constraint<T> =
182+
BetterTypeScriptLibInternals.StructuredClone.NeverOrUnknown<
183+
BetterTypeScriptLibInternals.StructuredClone.StructuredCloneOutput<
184+
BetterTypeScriptLibInternals.StructuredClone.AvoidCyclicConstraint<T>
185+
>
186+
>;
181187
}
182188
}
183189

184190
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/structuredClone) */
185191
declare function structuredClone<
186-
const T extends BetterTypeScriptLibInternals.StructuredClone.NeverOrUnknown<
187-
BetterTypeScriptLibInternals.StructuredClone.StructuredCloneOutput<
188-
BetterTypeScriptLibInternals.StructuredClone.AvoidCyclicConstraint<T>
189-
>
190-
>,
192+
const T extends BetterTypeScriptLibInternals.StructuredClone.Constraint<T>,
191193
>(
192194
value: T,
193195
options?: StructuredSerializeOptions,

tests/src/dom.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,25 @@ const test = async (url: string) => {
115115
// @ts-expect-error
116116
const p = structuredClone(() => 1);
117117
}
118+
// unions
119+
{
120+
function getData() {
121+
if (Math.random() > 0.5) {
122+
return { a: 5, b: null };
123+
} else {
124+
return { a: null, b: "hello" };
125+
}
126+
}
127+
expectType<{ a: number; b: null } | { a: null; b: string }>(
128+
structuredClone(getData()),
129+
);
130+
}
131+
// generic functions
132+
{
133+
function func1<
134+
T extends BetterTypeScriptLibInternals.StructuredClone.Constraint<T>,
135+
>(obj: T) {
136+
return structuredClone(obj);
137+
}
138+
}
118139
}

0 commit comments

Comments
 (0)