`Decode` is not type-safe
reviewgenericsdesign
```typescript
import Type from "typebox";
import Value from "typebox/value"
const FooOrBar = Type.Union([
Type.Object({ foo: Type.String() }),
Type.Object({ bar: Type.Number() })
]);
const bad: { bar: number } = Value.Decode(FooOrBar, { foo: "hello" });
console.log(bad); // prints { foo: "hello" } despite the type being { bar: number }
```
This compiles, despite clearly being wrong
https://www.typescriptlang.org/play/?target=99&moduleResolution=99#code/JYWwDg9gTgLgBAFQJ5gKZwGZQiOAiGFVAIwgA88BuAKFEljgDUBDAGwFd0sd9C1SyAegBubTnmrUAxhAB2AZ3gAxCBADyUAELMocALyIiAOgCqs4HIAUAbWpxDaI2uIArVFJiWA3plUAuB1QjAGUYKGBZAHNLAEo4AF8YgBo7QKdXd08fYh0A5EcAOXYQYlQoWISY6gBdGJppOUU4HIATAOzcuFli0t14-SYxIIARdwgW1EsVdS0dJLgfDH98AAtUVlYIPEr6mQUIViDN6Na6oA
`Decode`'s complex generic types allow for the return type to be any subtype of the StaticDecode type of the schema, which means you can narrow the type of the return value with checks or type errors. This is asking for bugs.
`Decode`'s type is currently
```typescript
function Decode<const Type extends TSchema, Result extends unknown = StaticDecode<Type>>(type: Type, value: unknown): Result
```
I believe making the return type not generic will fix this, but I don't know if there was a reason for including it
```typescript
function Decode<const Type extends TSchema>(type: Type, value: unknown): StaticDecode<Type>
```
2 条评论