|
1 | | -import { expectType } from "tsd"; |
| 1 | +import { expectNotType, expectType } from "tsd"; |
2 | 2 |
|
3 | 3 | const test = async (url: string) => { |
4 | 4 | const response = await fetch(url); |
5 | 5 | const json = await response.json(); |
6 | 6 | expectType<JSONValue>(json); |
7 | 7 | }; |
| 8 | + |
| 9 | +// structuredClone |
| 10 | +{ |
| 11 | + // primitives |
| 12 | + expectType<5>(structuredClone(5)); |
| 13 | + expectType<"hello">(structuredClone("hello")); |
| 14 | + expectType<true>(structuredClone(true)); |
| 15 | + expectType<undefined>(structuredClone(undefined)); |
| 16 | + expectType<null>(structuredClone(null)); |
| 17 | + // plain objects |
| 18 | + expectType<{ a: 5 }>(structuredClone({ a: 5 })); |
| 19 | + expectType<{ |
| 20 | + a: 5; |
| 21 | + nested: { |
| 22 | + b: "hello"; |
| 23 | + }; |
| 24 | + }>(structuredClone({ a: 5, nested: { b: "hello" } })); |
| 25 | + const obj = { a: 5 }; |
| 26 | + expectType<{ a: number }>(structuredClone(obj)); |
| 27 | + // arrays |
| 28 | + expectType<[1, 2, 3]>(structuredClone([1, 2, 3])); |
| 29 | + expectType<["a", "b", "c"]>(structuredClone(["a", "b", "c"])); |
| 30 | + const arr = [1, 2, 3]; |
| 31 | + expectType<number[]>(structuredClone(arr)); |
| 32 | + // read-onlyness is removed |
| 33 | + { |
| 34 | + const a: readonly number[] = [1, 2, 3]; |
| 35 | + const b = structuredClone(a); |
| 36 | + expectType<number[]>(b); |
| 37 | + b.push(4); |
| 38 | + } |
| 39 | + // TypedArrays |
| 40 | + expectType<Int16Array>(structuredClone(new Int16Array())); |
| 41 | + { |
| 42 | + // class instances are converted to base built-in types |
| 43 | + class Weirdo extends Int16Array { |
| 44 | + public weirdo: undefined = undefined; |
| 45 | + } |
| 46 | + |
| 47 | + class Weirdo2 extends Int32Array { |
| 48 | + public weirdo2: undefined = undefined; |
| 49 | + } |
| 50 | + |
| 51 | + expectType<Int16Array>(structuredClone(new Weirdo())); |
| 52 | + |
| 53 | + // @ts-expect-error property does not exist |
| 54 | + structuredClone(new Weirdo()).weirdo; |
| 55 | + const f: readonly [Weirdo] = [new Weirdo()] as const; |
| 56 | + expectType<[Int16Array]>(structuredClone(f)); |
| 57 | + // @ts-expect-error property does not exist |
| 58 | + structuredClone(f)[0].weirdo; |
| 59 | + |
| 60 | + const g = { a: new Weirdo() }; |
| 61 | + const g2 = structuredClone(g); |
| 62 | + expectType<{ a: Int16Array }>(g2); |
| 63 | + // @ts-expect-error property does not exist |
| 64 | + g2.a.weirdo; |
| 65 | + |
| 66 | + const h = new Map([[new Weirdo(), new Weirdo2()]]); |
| 67 | + const i = structuredClone(h); |
| 68 | + expectType<Map<Int16Array, Int32Array>>(i); |
| 69 | + expectNotType<Map<Weirdo, Weirdo2>>(i); |
| 70 | + |
| 71 | + const j = new Set([new Weirdo()]); |
| 72 | + const k: Set<Int16Array> = structuredClone(j); |
| 73 | + expectNotType<Set<Weirdo>>(k); |
| 74 | + |
| 75 | + class Empty {} |
| 76 | + expectType<{}>(structuredClone(new Empty())); |
| 77 | + class SingleProp { |
| 78 | + hello: number = 3; |
| 79 | + } |
| 80 | + expectType<{ hello: number }>(structuredClone(new SingleProp())); |
| 81 | + |
| 82 | + class WithConstructor { |
| 83 | + hi: number; |
| 84 | + constructor(hi: number) { |
| 85 | + this.hi = hi; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + expectType<{ hi: number }>(structuredClone(new WithConstructor(1))); |
| 90 | + |
| 91 | + class WithFunction { |
| 92 | + hello(): "hi" { |
| 93 | + return "hi"; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + expectType<{}>(structuredClone(new WithFunction())); |
| 98 | + // @ts-expect-error |
| 99 | + structuredClone(new WithFunction()).hello(); |
| 100 | + // ^? |
| 101 | + const x = structuredClone({ s: () => 1 }); |
| 102 | + // ^? |
| 103 | + } |
| 104 | + // non-clonable objects |
| 105 | + { |
| 106 | + // @ts-expect-error |
| 107 | + const m = structuredClone(class {}); |
| 108 | + // @ts-expect-error |
| 109 | + const n = structuredClone(Symbol.iterator); |
| 110 | + // @ts-expect-error |
| 111 | + const p = structuredClone(() => 1); |
| 112 | + } |
| 113 | +} |
0 commit comments