elysiajs / elysia

Ergonomic Framework for Humans
https://elysiajs.com
MIT License
10.6k stars 226 forks source link

typebox type missmatch with transform #875

Open K4leri opened 1 month ago

K4leri commented 1 month ago

What version of Elysia is running?

1.1.20

What platform is your computer?

Microsoft Windows NT 10.0.19045.0 x64

What steps can reproduce the bug?

const id = t
  .Transform(t.String())
  .Decode((value: string) => +value)
  .Encode((value: number) => String(value));

const someSchema = t.Object({
  id: Value.Encode(id, Value),
});

const someDecode = t.Object({
  id: Value.Decode(id, Value),
}),

изображение

What is the expected behavior?

const T = Type
  .Transform(Type.Number())
  .Decode((value) => new Date(value)) // decode: number to Date
  .Encode((value) => value.getTime()); // encode: Date to number

const D = Value.Decode(T, 0); // const D = Date(1970-01-01T00:00:00.000Z)
const E = Value.Encode(T, D);

no errors such as in original typebox example

What do you see instead?

Argument of type 'TTransform<TString, number>' is not assignable to parameter of type 'TSchema'.
  Property '[Kind]' is missing in type 'TTransform<TString, number>' but required in type 'TSchema'.ts(2345)
schema.d.ts(21, 5): '[Kind]' is declared here.

Additional information

I am trying to make type casting in returning values. I would like to obfuacate my id from data base from Integers to Strings. I think that its possible to make it with typebox cause it do the same thing in incoming body, query, params requests. It make typecasting with some values such as integers.

I even tried to make this and it still doesnt work

// package.json
  "override": {
    "@sinclair/typebox": "^0.33.15"
  }

Very close to https://github.com/elysiajs/elysia/issues/873

Have you try removing the node_modules and bun.lockb and try again yet?

yes

K4leri commented 1 month ago
  "overrides": {
    "@sinclair/typebox": "0.32.34"
  }

this fix the problem but not fix the general goal with type casting key