You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -287,6 +287,11 @@ that it requires the value to be of type:
287
287
*/
288
288
````
289
289
You can even add explicit `VALUE` generics to make the compiler check that your `Type` serializes the correct types of values.
290
+
Many non-primitive types (e.g. `ArrayType`, `MapType`, `StructType`) can take either one or two generics.
291
+
The first, `VALUE`, specifies the type of values the type can write.
292
+
The second, `READ_VALUE`, specifies the type of values the type will read from written values and defaults to `VALUE` if omitted.
293
+
Generally the two generics are the same, except in the case of numeric types (which write `number | string` but read `number`) and `OptionalType` (which writes `E | null | undefined` but reads `E | null`).
294
+
`READ_VALUE` must be a subset of `VALUE`, so in most cases where you specify the generic it makes sense to restrict `VALUE` to the type of read values and let `READ_VALUE` default to `VALUE`.
290
295
291
296
With `StructType`:
292
297
````typescript
@@ -328,10 +333,11 @@ let structType = new sb.StructType<SerializedCar>({
328
333
329
334
With `ChoiceType` (and similarly for `NamedChoiceType`), you can let the type be inferred automatically if each of the possible types writes the same type of values:
330
335
````javascript
331
-
let type =newsb.ChoiceType([ //Type<number | string>
336
+
let type =newsb.ChoiceType([ //Type<number | string, number>
332
337
newsb.ByteType,
333
338
newsb.ShortType,
334
-
newsb.IntType
339
+
newsb.IntType,
340
+
newsb.DoubleType
335
341
])
336
342
````
337
343
However, if the value types are not the same, TypeScript will complain about them not matching, and you should express the union type explicitly:
0 commit comments