deepkit / deepkit-framework

A new full-featured and high-performance TypeScript framework
https://deepkit.io/
MIT License
3.14k stars 116 forks source link

"Getting started" steps for Runtime Types isn't working #581

Closed micalevisk closed 2 weeks ago

micalevisk commented 2 weeks ago

After following the steps at https://deepkit.io/documentation/runtime-types/getting-started (everything before the Type compiler section) I'm seeing this error:

return new TSError(diagnosticText, diagnosticCodes, diagnostics);
           ^
TSError: ⨯ Unable to compile TypeScript:
app.ts:8:25 - error TS2693: 'User' only refers to a type, but is being used as a value here.

8 const user = cast<User>(User, {
                          ~~~~
app.ts:9:5 - error TS2353: Object literal may only specify known properties, and 'username' does not exist in type 'SerializationOptions'.

9     username: 'Peter',
      ~~~~~~~~
app.ts:14:41 - error TS2693: 'User' only refers to a type, but is being used as a value here.

14 const reflection = ReflectionClass.from(User);
                                           ~~~~

The repro: https://gitlab.com/micalevisk/deepkit-runtime-type-starter-issue

Steps to reproduce:

git clone https://gitlab.com/micalevisk/deepkit-runtime-type-starter-issue.git
cd deepkit-runtime-type-starter-issue
npm i
./node_modules/.bin/ts-node app.ts

running node_modules/.bin/deepkit-type-install didn't fix it neither.

marcus-sa commented 2 weeks ago

You're trying to use a type as a value. User is an interface not a class.

The correct code would be

import { cast, MinLength, ReflectionClass } from '@deepkit/type';

interface User {
    username: string & MinLength<3>;
    birthDate?: Date;
}

const user = cast<User>({
    username: 'Peter',
    birthDate: '2010-10-10T00:00:00Z'
});
console.log(user);

const reflection = ReflectionClass.from<User>();
console.log(reflection.getProperty('username').type);
micalevisk commented 2 weeks ago

ty

559 will fix that