jazz-tools 0.8.3 → 0.8.5
Sign up to get free protection for your applications and to get access to all the features.
- package/.turbo/turbo-build.log +11 -3
- package/CHANGELOG.md +10 -0
- package/dist/native/implementation/schema.js +14 -9
- package/dist/native/implementation/schema.js.map +1 -1
- package/dist/web/implementation/schema.js +14 -9
- package/dist/web/implementation/schema.js.map +1 -1
- package/package.json +6 -6
- package/src/implementation/schema.ts +21 -13
- package/src/tests/coMap.test.ts +1 -1
- package/src/tests/schema.test.ts +205 -0
package/.turbo/turbo-build.log
CHANGED
@@ -1,8 +1,16 @@
|
|
1
1
|
|
2
|
-
> jazz-tools@0.8.
|
3
|
-
> npm run lint && rm -rf ./dist &&
|
2
|
+
> jazz-tools@0.8.3 build /Users/anselm/jazz/jazz/packages/jazz-tools
|
3
|
+
> npm run lint && rm -rf ./dist && pnpm run build:web && pnpm run build:native
|
4
4
|
|
5
5
|
|
6
|
-
> jazz-tools@0.8.
|
6
|
+
> jazz-tools@0.8.3 lint
|
7
7
|
> eslint . --ext ts,tsx
|
8
8
|
|
9
|
+
|
10
|
+
> jazz-tools@0.8.3 build:web /Users/anselm/jazz/jazz/packages/jazz-tools
|
11
|
+
> tsc --sourceMap --outDir dist/web -p tsconfig.web.json
|
12
|
+
|
13
|
+
|
14
|
+
> jazz-tools@0.8.3 build:native /Users/anselm/jazz/jazz/packages/jazz-tools
|
15
|
+
> tsc --sourceMap --outDir dist/native -p tsconfig.native.json
|
16
|
+
|
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,15 @@
|
|
1
1
|
# jazz-tools
|
2
2
|
|
3
|
+
## 0.8.5
|
4
|
+
|
5
|
+
### Patch Changes
|
6
|
+
|
7
|
+
- c3f4e6b: Fix order of exports fields in package.json
|
8
|
+
- d9152ed: Allow interface types as generic argument in co.json
|
9
|
+
- Updated dependencies [c3f4e6b]
|
10
|
+
- Updated dependencies [d9152ed]
|
11
|
+
- cojson@0.8.5
|
12
|
+
|
3
13
|
## 0.8.3
|
4
14
|
|
5
15
|
### Patch Changes
|
@@ -1,4 +1,11 @@
|
|
1
|
-
import { isCoValueClass, } from "../internal.js";
|
1
|
+
import { isCoValueClass, SchemaInit, ItemsSym, MembersSym, } from "../internal.js";
|
2
|
+
/** @category Schema definition */
|
3
|
+
export const Encoders = {
|
4
|
+
Date: {
|
5
|
+
encode: (value) => value.toISOString(),
|
6
|
+
decode: (value) => new Date(value),
|
7
|
+
},
|
8
|
+
};
|
2
9
|
const optional = {
|
3
10
|
ref: optionalRef,
|
4
11
|
json() {
|
@@ -21,6 +28,9 @@ const optional = {
|
|
21
28
|
null: {
|
22
29
|
[SchemaInit]: "json",
|
23
30
|
},
|
31
|
+
Date: {
|
32
|
+
[SchemaInit]: { encoded: Encoders.Date },
|
33
|
+
},
|
24
34
|
literal(..._lit) {
|
25
35
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
26
36
|
return { [SchemaInit]: "json" };
|
@@ -40,6 +50,9 @@ export const co = {
|
|
40
50
|
null: {
|
41
51
|
[SchemaInit]: "json",
|
42
52
|
},
|
53
|
+
Date: {
|
54
|
+
[SchemaInit]: { encoded: Encoders.Date },
|
55
|
+
},
|
43
56
|
literal(..._lit) {
|
44
57
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
45
58
|
return { [SchemaInit]: "json" };
|
@@ -80,12 +93,4 @@ export function instantiateRefEncoded(schema, raw) {
|
|
80
93
|
? schema.ref.fromRaw(raw)
|
81
94
|
: schema.ref(raw).fromRaw(raw);
|
82
95
|
}
|
83
|
-
import { SchemaInit, ItemsSym, MembersSym } from "./symbols.js";
|
84
|
-
/** @category Schema definition */
|
85
|
-
export const Encoders = {
|
86
|
-
Date: {
|
87
|
-
encode: (value) => value.toISOString(),
|
88
|
-
decode: (value) => new Date(value),
|
89
|
-
},
|
90
|
-
};
|
91
96
|
//# sourceMappingURL=schema.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../../src/implementation/schema.ts"],"names":[],"mappings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QACpB,OAAO,MAAM,CAAC,GAAG,KAAK,UAAU,CACnC,CAAC;AACN,CAAC;AAED,MAAM,UAAU,qBAAqB,CACjC,MAAqB,EACrB,GAAe;IAEf,OAAO,cAAc,CAAI,MAAM,CAAC,GAAG,CAAC;QAChC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC;QACzB,CAAC,CACK,MAAM,CAAC,GAGV,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;AAC9B,CAAC"}
|
@@ -1,4 +1,11 @@
|
|
1
|
-
import { isCoValueClass, } from "../internal.js";
|
1
|
+
import { isCoValueClass, SchemaInit, ItemsSym, MembersSym, } from "../internal.js";
|
2
|
+
/** @category Schema definition */
|
3
|
+
export const Encoders = {
|
4
|
+
Date: {
|
5
|
+
encode: (value) => value.toISOString(),
|
6
|
+
decode: (value) => new Date(value),
|
7
|
+
},
|
8
|
+
};
|
2
9
|
const optional = {
|
3
10
|
ref: optionalRef,
|
4
11
|
json() {
|
@@ -21,6 +28,9 @@ const optional = {
|
|
21
28
|
null: {
|
22
29
|
[SchemaInit]: "json",
|
23
30
|
},
|
31
|
+
Date: {
|
32
|
+
[SchemaInit]: { encoded: Encoders.Date },
|
33
|
+
},
|
24
34
|
literal(..._lit) {
|
25
35
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
26
36
|
return { [SchemaInit]: "json" };
|
@@ -40,6 +50,9 @@ export const co = {
|
|
40
50
|
null: {
|
41
51
|
[SchemaInit]: "json",
|
42
52
|
},
|
53
|
+
Date: {
|
54
|
+
[SchemaInit]: { encoded: Encoders.Date },
|
55
|
+
},
|
43
56
|
literal(..._lit) {
|
44
57
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
45
58
|
return { [SchemaInit]: "json" };
|
@@ -80,12 +93,4 @@ export function instantiateRefEncoded(schema, raw) {
|
|
80
93
|
? schema.ref.fromRaw(raw)
|
81
94
|
: schema.ref(raw).fromRaw(raw);
|
82
95
|
}
|
83
|
-
import { SchemaInit, ItemsSym, MembersSym } from "./symbols.js";
|
84
|
-
/** @category Schema definition */
|
85
|
-
export const Encoders = {
|
86
|
-
Date: {
|
87
|
-
encode: (value) => value.toISOString(),
|
88
|
-
decode: (value) => new Date(value),
|
89
|
-
},
|
90
|
-
};
|
91
96
|
//# sourceMappingURL=schema.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../../src/implementation/schema.ts"],"names":[],"mappings":"AACA,OAAO,EAGH,cAAc,
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../../src/implementation/schema.ts"],"names":[],"mappings":"AACA,OAAO,EAGH,cAAc,EAEd,UAAU,EACV,QAAQ,EACR,UAAU,GACb,MAAM,gBAAgB,CAAC;AAGxB,kCAAkC;AAClC,MAAM,CAAC,MAAM,QAAQ,GAAG;IACpB,IAAI,EAAE;QACF,MAAM,EAAE,CAAC,KAAW,EAAE,EAAE,CAAC,KAAK,CAAC,WAAW,EAAE;QAC5C,MAAM,EAAE,CAAC,KAAgB,EAAE,EAAE,CAAC,IAAI,IAAI,CAAC,KAAe,CAAC;KAC1D;CACJ,CAAC;AAYF,MAAM,QAAQ,GAAG;IACb,GAAG,EAAE,WAAW;IAChB,IAAI;QACA,8DAA8D;QAC9D,OAAO,EAAE,CAAC,UAAU,CAAC,EAAE,MAAuB,EAAS,CAAC;IAC5D,CAAC;IACD,OAAO,CAAI,GAAuB;QAC9B,8DAA8D;QAC9D,OAAO,EAAE,CAAC,UAAU,CAAC,EAAE,EAAE,OAAO,EAAE,GAAG,EAAmB,EAAS,CAAC;IACtE,CAAC;IACD,MAAM,EAAE;QACJ,CAAC,UAAU,CAAC,EAAE,MAAuB;KACH;IACtC,MAAM,EAAE;QACJ,CAAC,UAAU,CAAC,EAAE,MAAuB;KACH;IACtC,OAAO,EAAE;QACL,CAAC,UAAU,CAAC,EAAE,MAAuB;KACF;IACvC,IAAI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AUD,SAAS,GAAG,CAIR,GAA+C,EAC/C,OAAiB;IAIjB,OAAO;QACH,CAAC,UAAU,CAAC,EAAE;YACV,GAAG,EAAE,GAAG;YACR,QAAQ,EAAE,OAAO,EAAE,QAAQ,IAAI,KAAK;SACtB;QAClB,8DAA8D;KAC1D,CAAC;AACb,CAAC;AASD,MAAM,UAAU,YAAY,CACxB,MAAc;IAEd,OAAO,CACH,OAAO,MAAM,KAAK,QAAQ;QAC1B,KAAK,IAAI,MAAM;QACf,UAAU,IAAI,MAAM;QACpB,OAAO,MAAM,CAAC,GAAG,KAAK,UAAU,CACnC,CAAC;AACN,CAAC;AAED,MAAM,UAAU,qBAAqB,CACjC,MAAqB,EACrB,GAAe;IAEf,OAAO,cAAc,CAAI,MAAM,CAAC,GAAG,CAAC;QAChC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC;QACzB,CAAC,CACK,MAAM,CAAC,GAGV,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;AAC9B,CAAC"}
|
package/package.json
CHANGED
@@ -7,22 +7,22 @@
|
|
7
7
|
"exports": {
|
8
8
|
".": {
|
9
9
|
"react-native": "./dist/native/index.native.js",
|
10
|
-
"
|
11
|
-
"
|
10
|
+
"types": "./src/index.web.ts",
|
11
|
+
"default": "./dist/web/index.web.js"
|
12
12
|
},
|
13
13
|
"./native": {
|
14
14
|
"react-native": "./dist/native/index.native.js",
|
15
|
-
"
|
16
|
-
"
|
15
|
+
"types": "./src/index.native.ts",
|
16
|
+
"default": "./dist/native/index.native.js"
|
17
17
|
},
|
18
18
|
"./src/*": "./src/*"
|
19
19
|
},
|
20
20
|
"type": "module",
|
21
21
|
"license": "MIT",
|
22
|
-
"version": "0.8.
|
22
|
+
"version": "0.8.5",
|
23
23
|
"dependencies": {
|
24
24
|
"fast-check": "^3.17.2",
|
25
|
-
"cojson": "0.8.
|
25
|
+
"cojson": "0.8.5"
|
26
26
|
},
|
27
27
|
"devDependencies": {
|
28
28
|
"typescript": "^5.3.3",
|
@@ -4,7 +4,19 @@ import {
|
|
4
4
|
type CoValueClass,
|
5
5
|
isCoValueClass,
|
6
6
|
CoValueFromRaw,
|
7
|
+
SchemaInit,
|
8
|
+
ItemsSym,
|
9
|
+
MembersSym,
|
7
10
|
} from "../internal.js";
|
11
|
+
import { CoJsonValue } from "cojson/src/jsonValue.js";
|
12
|
+
|
13
|
+
/** @category Schema definition */
|
14
|
+
export const Encoders = {
|
15
|
+
Date: {
|
16
|
+
encode: (value: Date) => value.toISOString(),
|
17
|
+
decode: (value: JsonValue) => new Date(value as string),
|
18
|
+
},
|
19
|
+
};
|
8
20
|
|
9
21
|
export type CoMarker = { readonly __co: unique symbol };
|
10
22
|
/** @category Schema definition */
|
@@ -18,7 +30,7 @@ export type UnCo<T> = T extends co<infer A> ? A : T;
|
|
18
30
|
|
19
31
|
const optional = {
|
20
32
|
ref: optionalRef,
|
21
|
-
json<T extends
|
33
|
+
json<T extends CoJsonValue<T>>(): co<T | undefined> {
|
22
34
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
23
35
|
return { [SchemaInit]: "json" satisfies Schema } as any;
|
24
36
|
},
|
@@ -38,6 +50,9 @@ const optional = {
|
|
38
50
|
null: {
|
39
51
|
[SchemaInit]: "json" satisfies Schema,
|
40
52
|
} as unknown as co<null | undefined>,
|
53
|
+
Date: {
|
54
|
+
[SchemaInit]: { encoded: Encoders.Date } satisfies Schema,
|
55
|
+
} as unknown as co<Date | undefined>,
|
41
56
|
literal<T extends (string | number | boolean)[]>(
|
42
57
|
..._lit: T
|
43
58
|
): co<T[number] | undefined> {
|
@@ -60,13 +75,16 @@ export const co = {
|
|
60
75
|
null: {
|
61
76
|
[SchemaInit]: "json" satisfies Schema,
|
62
77
|
} as unknown as co<null>,
|
78
|
+
Date: {
|
79
|
+
[SchemaInit]: { encoded: Encoders.Date } satisfies Schema,
|
80
|
+
} as unknown as co<Date>,
|
63
81
|
literal<T extends (string | number | boolean)[]>(
|
64
82
|
..._lit: T
|
65
83
|
): co<T[number]> {
|
66
84
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
67
85
|
return { [SchemaInit]: "json" satisfies Schema } as any;
|
68
86
|
},
|
69
|
-
json<T extends
|
87
|
+
json<T extends CoJsonValue<T>>(): co<T> {
|
70
88
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
71
89
|
return { [SchemaInit]: "json" satisfies Schema } as any;
|
72
90
|
},
|
@@ -155,20 +173,10 @@ export type SchemaFor<Field> = NonNullable<Field> extends CoValue
|
|
155
173
|
export type Encoder<V> = {
|
156
174
|
encode: (value: V) => JsonValue;
|
157
175
|
decode: (value: JsonValue) => V;
|
158
|
-
}
|
176
|
+
}
|
159
177
|
export type OptionalEncoder<V> =
|
160
178
|
| Encoder<V>
|
161
179
|
| {
|
162
180
|
encode: (value: V | undefined) => JsonValue;
|
163
181
|
decode: (value: JsonValue) => V | undefined;
|
164
182
|
};
|
165
|
-
|
166
|
-
import { SchemaInit, ItemsSym, MembersSym } from "./symbols.js";
|
167
|
-
|
168
|
-
/** @category Schema definition */
|
169
|
-
export const Encoders = {
|
170
|
-
Date: {
|
171
|
-
encode: (value: Date) => value.toISOString(),
|
172
|
-
decode: (value: JsonValue) => new Date(value as string),
|
173
|
-
},
|
174
|
-
};
|
package/src/tests/coMap.test.ts
CHANGED
@@ -18,7 +18,7 @@ const Crypto = await WasmCrypto.create();
|
|
18
18
|
class TestMap extends CoMap {
|
19
19
|
color = co.string;
|
20
20
|
_height = co.number;
|
21
|
-
birthday = co.
|
21
|
+
birthday = co.Date;
|
22
22
|
name? = co.string;
|
23
23
|
nullable = co.optional.encoded<string | undefined>({
|
24
24
|
encode: (value: string | undefined) => value || null,
|
@@ -0,0 +1,205 @@
|
|
1
|
+
import { describe, expectTypeOf, it } from "vitest";
|
2
|
+
import { CoMap, co } from "../index.web.js";
|
3
|
+
import { co as valueWithCoMarker } from "../internal.js";
|
4
|
+
|
5
|
+
describe("co.json TypeScript validation", () => {
|
6
|
+
it("should accept serializable types", async () => {
|
7
|
+
type ValidType = { str: string; num: number; bool: boolean };
|
8
|
+
|
9
|
+
class ValidPrimitiveMap extends CoMap {
|
10
|
+
data = co.json<ValidType>();
|
11
|
+
}
|
12
|
+
|
13
|
+
expectTypeOf(ValidPrimitiveMap.create<ValidPrimitiveMap>)
|
14
|
+
.parameter(0)
|
15
|
+
.toEqualTypeOf<{
|
16
|
+
data: valueWithCoMarker<ValidType>;
|
17
|
+
}>();
|
18
|
+
});
|
19
|
+
|
20
|
+
it("should accept nested serializable types", async () => {
|
21
|
+
type NestedType = {
|
22
|
+
outer: {
|
23
|
+
inner: {
|
24
|
+
value: string;
|
25
|
+
};
|
26
|
+
};
|
27
|
+
}
|
28
|
+
|
29
|
+
class ValidNestedMap extends CoMap {
|
30
|
+
data = co.json<NestedType>();
|
31
|
+
}
|
32
|
+
|
33
|
+
expectTypeOf(ValidNestedMap.create<ValidNestedMap>)
|
34
|
+
.parameter(0)
|
35
|
+
.toEqualTypeOf<{
|
36
|
+
data: valueWithCoMarker<NestedType>;
|
37
|
+
}>();
|
38
|
+
});
|
39
|
+
|
40
|
+
it("should accept types with optional attributes", async () => {
|
41
|
+
type TypeWithOptional = {
|
42
|
+
value: string;
|
43
|
+
optional?: string | null;
|
44
|
+
}
|
45
|
+
|
46
|
+
class ValidMap extends CoMap {
|
47
|
+
data = co.json<TypeWithOptional>();
|
48
|
+
}
|
49
|
+
|
50
|
+
expectTypeOf(ValidMap.create<ValidMap>)
|
51
|
+
.parameter(0)
|
52
|
+
.toEqualTypeOf<{
|
53
|
+
data: valueWithCoMarker<TypeWithOptional>;
|
54
|
+
}>();
|
55
|
+
});
|
56
|
+
|
57
|
+
it("should accept nested serializable interfaces", async () => {
|
58
|
+
interface InnerInterface {
|
59
|
+
value: string;
|
60
|
+
}
|
61
|
+
|
62
|
+
interface NestedInterface {
|
63
|
+
outer: {
|
64
|
+
inner: InnerInterface;
|
65
|
+
};
|
66
|
+
}
|
67
|
+
|
68
|
+
class ValidNestedMap extends CoMap {
|
69
|
+
data = co.json<NestedInterface>();
|
70
|
+
}
|
71
|
+
|
72
|
+
expectTypeOf(ValidNestedMap.create<ValidNestedMap>)
|
73
|
+
.parameter(0)
|
74
|
+
.toEqualTypeOf<{
|
75
|
+
data: valueWithCoMarker<NestedInterface>;
|
76
|
+
}>();
|
77
|
+
});
|
78
|
+
|
79
|
+
it("should accept arrays of serializable types", async () => {
|
80
|
+
interface ArrayInterface {
|
81
|
+
numbers: number[];
|
82
|
+
objects: { id: number; name: string }[];
|
83
|
+
}
|
84
|
+
|
85
|
+
class ValidArrayMap extends CoMap {
|
86
|
+
data = co.json<ArrayInterface>();
|
87
|
+
}
|
88
|
+
|
89
|
+
expectTypeOf(ValidArrayMap.create<ValidArrayMap>)
|
90
|
+
.parameter(0)
|
91
|
+
.toEqualTypeOf<{
|
92
|
+
data: valueWithCoMarker<ArrayInterface>;
|
93
|
+
}>();
|
94
|
+
});
|
95
|
+
|
96
|
+
it("should flag interfaces with functions as invalid", async () => {
|
97
|
+
interface InvalidInterface {
|
98
|
+
func: () => void;
|
99
|
+
}
|
100
|
+
|
101
|
+
class InvalidFunctionMap extends CoMap {
|
102
|
+
// @ts-expect-error Should not be considered valid
|
103
|
+
data = co.json<InvalidInterface>();
|
104
|
+
}
|
105
|
+
|
106
|
+
expectTypeOf(InvalidFunctionMap.create<InvalidFunctionMap>)
|
107
|
+
.parameter(0)
|
108
|
+
.toEqualTypeOf<{
|
109
|
+
data: valueWithCoMarker<InvalidInterface>;
|
110
|
+
}>();
|
111
|
+
});
|
112
|
+
|
113
|
+
it("should flag types with functions as invalid", async () => {
|
114
|
+
type InvalidType = { func: () => void };
|
115
|
+
|
116
|
+
class InvalidFunctionMap extends CoMap {
|
117
|
+
// @ts-expect-error Should not be considered valid
|
118
|
+
data = co.json<InvalidType>();
|
119
|
+
}
|
120
|
+
|
121
|
+
expectTypeOf(InvalidFunctionMap.create<InvalidFunctionMap>)
|
122
|
+
.parameter(0)
|
123
|
+
.toEqualTypeOf<{
|
124
|
+
data: valueWithCoMarker<InvalidType>;
|
125
|
+
}>();
|
126
|
+
});
|
127
|
+
|
128
|
+
it("should flag types with non-serializable constructors as invalid", async () => {
|
129
|
+
type InvalidType = { date: Date; regexp: RegExp; symbol: symbol };
|
130
|
+
|
131
|
+
class InvalidFunctionMap extends CoMap {
|
132
|
+
// @ts-expect-error Should not be considered valid
|
133
|
+
data = co.json<InvalidType>();
|
134
|
+
}
|
135
|
+
|
136
|
+
expectTypeOf(InvalidFunctionMap.create<InvalidFunctionMap>)
|
137
|
+
.parameter(0)
|
138
|
+
.toEqualTypeOf<{
|
139
|
+
data: valueWithCoMarker<InvalidType>;
|
140
|
+
}>();
|
141
|
+
});
|
142
|
+
|
143
|
+
it("should apply the same validation to optional json", async () => {
|
144
|
+
type ValidType = {
|
145
|
+
value: string;
|
146
|
+
};
|
147
|
+
|
148
|
+
type InvalidType = {
|
149
|
+
value: () => string;
|
150
|
+
};
|
151
|
+
|
152
|
+
class MapWithOptionalJSON extends CoMap {
|
153
|
+
data = co.optional.json<ValidType>();
|
154
|
+
// @ts-expect-error Should not be considered valid
|
155
|
+
data2 = co.optional.json<InvalidType>();
|
156
|
+
}
|
157
|
+
|
158
|
+
expectTypeOf(MapWithOptionalJSON.create<MapWithOptionalJSON>)
|
159
|
+
.parameter(0)
|
160
|
+
.toEqualTypeOf<{
|
161
|
+
data?: valueWithCoMarker<ValidType> | null;
|
162
|
+
data2?: valueWithCoMarker<InvalidType> | null;
|
163
|
+
}>();
|
164
|
+
});
|
165
|
+
|
166
|
+
it("should not accept functions", async () => {
|
167
|
+
class InvalidFunctionMap extends CoMap {
|
168
|
+
// @ts-expect-error Should not be considered valid
|
169
|
+
data = co.json<() => void>();
|
170
|
+
}
|
171
|
+
|
172
|
+
expectTypeOf(InvalidFunctionMap.create<InvalidFunctionMap>)
|
173
|
+
.parameter(0)
|
174
|
+
.toEqualTypeOf<{
|
175
|
+
data: valueWithCoMarker<() => void>;
|
176
|
+
}>();
|
177
|
+
});
|
178
|
+
|
179
|
+
it("should not accept RegExp", async () => {
|
180
|
+
class InvalidFunctionMap extends CoMap {
|
181
|
+
// @ts-expect-error Should not be considered valid
|
182
|
+
data = co.json<RegExp>();
|
183
|
+
}
|
184
|
+
|
185
|
+
expectTypeOf(InvalidFunctionMap.create<InvalidFunctionMap>)
|
186
|
+
.parameter(0)
|
187
|
+
.toEqualTypeOf<{
|
188
|
+
data: valueWithCoMarker<RegExp>;
|
189
|
+
}>();
|
190
|
+
});
|
191
|
+
|
192
|
+
it("should accept strings and numbers", async () => {
|
193
|
+
class InvalidFunctionMap extends CoMap {
|
194
|
+
str = co.json<string>();
|
195
|
+
num = co.json<number>();
|
196
|
+
}
|
197
|
+
|
198
|
+
expectTypeOf(InvalidFunctionMap.create<InvalidFunctionMap>)
|
199
|
+
.parameter(0)
|
200
|
+
.toEqualTypeOf<{
|
201
|
+
str: valueWithCoMarker<string>;
|
202
|
+
num: valueWithCoMarker<number>;
|
203
|
+
}>();
|
204
|
+
});
|
205
|
+
});
|