Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ttypescript (ttsc) #401

Closed
mindplay-dk opened this issue Jan 20, 2023 · 6 comments
Closed

Add support for ttypescript (ttsc) #401

mindplay-dk opened this issue Jan 20, 2023 · 6 comments
Labels
enhancement New feature or request
Milestone

Comments

@mindplay-dk
Copy link

Are you aware of ttypescript (ttsc)? two t's, not a typo 😅

I don't see any mentions in either issues or discussions, so I thought I'd bring it up.

I don't know if DeepKit is mainly a set of TypeScript transformers, or if there's more going on?

If it would work with just the two transformers, ttsc might be a way to avoid doing the install-time transformations you're doing now - and also would provide access to a small ecosystem of other transforms.

ttsc already has integrations with tools like WebPack, RollUp, Parcel and VS Code, so this might also be a way to make DeepKit more generally available to the wider JS ecosystem and get some more exposure.

I haven't really looked into it yet, but if the transforms are already exported, it might work with the ttsc already, with the right configuration?

I figured I'd ask before spending time on it though - I figured the developers might already have considered that and maybe there's some reason this isn't going to work?

@marcj
Copy link
Member

marcj commented Apr 13, 2023

Good idea, let's try it

@marcj marcj added the enhancement New feature or request label Apr 13, 2023
@marcj marcj added this to the Beta milestone Apr 15, 2023
marcj added a commit that referenced this issue May 6, 2023
@marcj
Copy link
Member

marcj commented May 8, 2023

This is supported now since v1.0.1-alpha.94.

example tsconfig.json:

{
    "compilerOptions": {
        "plugins": [
            { "transform": "@deepkit/type-compiler" }
        ]
    },
    "reflection": true
}

@marcj marcj closed this as completed May 8, 2023
@mindplay-dk
Copy link
Author

@marcj what are the dependencies, requirements and settings to get this going? (do you have an example?)

I tried it with a basic tsconfig.json:

{
  "reflection": true, // DeepKit
  "compilerOptions": {
    "plugins": [
      { "transform": "@deepkit/type-compiler" }
    ],
    "target": "ESNext",                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
    "module": "NodeNext",                                /* Specify what module code is generated. */
    "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
    "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */
    "strict": true,                                      /* Enable all strict type-checking options. */
    "skipLibCheck": true                                 /* Skip type checking all .d.ts files. */
  }
}

And package.json:

{
  "name": "try-deepkit",
  "scripts": {
    "start": "ttsc src/hello.ts"
  },
  "devDependencies": {
    "@deepkit/type-compiler": "^1.0.1-alpha.97",
    "ttypescript": "^1.5.15",
    "typescript": "^5.0.4"
  }
}

My src/hello.ts is just console.log("hello").

At npm start, I see the following:

$ npm start

> start
> ttsc src/hello.ts

/home/mindplay/workspace/try-deepkit/node_modules/ttypescript/lib/patchCreateProgram.js:84
    tsm.createProgram = createProgram;
                      ^

TypeError: Cannot set property createProgram of #<Object> which has only a getter
    at patchCreateProgram (/home/mindplay/workspace/try-deepkit/node_modules/ttypescript/lib/patchCreateProgram.js:84:23)
    at loadTypeScript (/home/mindplay/workspace/try-deepkit/node_modules/ttypescript/lib/loadTypescript.js:21:56)
    at Object.<anonymous> (/home/mindplay/workspace/try-deepkit/node_modules/ttypescript/lib/tsc.js:8:46)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)
    at Module.require (node:internal/modules/cjs/loader:1141:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at Object.<anonymous> (/home/mindplay/workspace/try-deepkit/node_modules/ttypescript/bin/tsc:2:1)

Node.js v18.14.2

@marcj
Copy link
Member

marcj commented May 13, 2023

@mindplay-dk you error refers to an incompatibility of ttransformer with TypeScript 5. See their repository for more information , cevek/ttypescript#147. It seems it is unmaintained though

@mindplay-dk
Copy link
Author

I see, yeah - it looks like people are migrating to ts-patch, which should support v5 with the latest beta - so I decided to give that a shot.

Updated package.json:

{
  "name": "try-deepkit",
  "scripts": {
    "prepare": "ts-patch install -s",
    "start": "tspc src/hello.ts"
  },
  "devDependencies": {
    "@deepkit/type-compiler": "^1.0.1-alpha.97",
    "typescript": "^5.0.4"
  },
  "dependencies": {
    "@deepkit/type": "^1.0.1-alpha.97",
    "ts-patch": "^3.0.0-beta3"
  }
}

It now appears to actually run the DeepKit transformer - but now that errors:

$ npm start

> start
> tspc src/hello.ts

node_modules/@deepkit/core/dist/cjs/src/compiler.d.ts:2:23 - error TS2583: Cannot find name 'Map'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.

2     readonly context: Map<string, any>;
                        ~~~

node_modules/@deepkit/core/dist/cjs/src/compiler.d.ts:3:31 - error TS2583: Cannot find name 'Map'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.

3     protected constVariables: Map<any, string>;
                                ~~~

node_modules/@deepkit/core/dist/cjs/src/compiler.d.ts:5:30 - error TS2583: Cannot find name 'Set'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.

5     protected reservedNames: Set<string>;
                               ~~~

node_modules/@deepkit/core/dist/cjs/src/core.d.ts:348:85 - error TS2304: Cannot find name 'IterableIterator'.

348 export declare function range(startOrLength: number, stop?: number, step?: number): IterableIterator<number>;
                                                                                        ~~~~~~~~~~~~~~~~

node_modules/@deepkit/core/dist/cjs/src/enum.d.ts:21:66 - error TS2583: Cannot find name 'Map'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.

21 export declare function getEnumKeyLabelMap(enumDefinition: any): Map<any, string>;
                                                                    ~~~

node_modules/@deepkit/core/dist/cjs/src/iterators.d.ts:14:59 - error TS2304: Cannot find name 'IterableIterator'.

14 export declare function eachKey<T>(object: ArrayLike<T>): IterableIterator<number>;
                                                             ~~~~~~~~~~~~~~~~

node_modules/@deepkit/core/dist/cjs/src/iterators.d.ts:18:35 - error TS2304: Cannot find name 'IterableIterator'.

18 }, K extends keyof T>(object: T): IterableIterator<string>;
                                     ~~~~~~~~~~~~~~~~

node_modules/@deepkit/core/dist/cjs/src/iterators.d.ts:34:20 - error TS2304: Cannot find name 'IterableIterator'.

34 } | ArrayLike<T>): IterableIterator<T>;
                      ~~~~~~~~~~~~~~~~

node_modules/@deepkit/core/dist/cjs/src/iterators.d.ts:52:60 - error TS2304: Cannot find name 'IterableIterator'.

52 export declare function eachPair<T>(object: ArrayLike<T>): IterableIterator<[number, T]>;
                                                              ~~~~~~~~~~~~~~~~

node_modules/@deepkit/core/dist/cjs/src/iterators.d.ts:56:5 - error TS2304: Cannot find name 'IterableIterator'.

56 }): IterableIterator<[string, T]>;
       ~~~~~~~~~~~~~~~~

node_modules/@deepkit/core/dist/cjs/src/reactive.d.ts:1:23 - error TS2688: Cannot find type definition file for 'node'.

1 /// <reference types="node" />
                        ~~~~

node_modules/@deepkit/core/dist/cjs/src/reactive.d.ts:2:84 - error TS2503: Cannot find namespace 'NodeJS'.

2 export declare const nextTick: ((cb: () => void) => number) | ((cb: () => void) => NodeJS.Timeout);
                                                                                     ~~~~~~

node_modules/@deepkit/type/dist/cjs/src/core.d.ts:1:23 - error TS2688: Cannot find type definition file for 'node'.

1 /// <reference types="node" />
                        ~~~~

node_modules/@deepkit/type/dist/cjs/src/registry.d.ts:11:14 - error TS2583: Cannot find name 'Map'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.

11     classes: Map<ClassType<any>, T>;
                ~~~

node_modules/@deepkit/type/dist/cjs/src/serializer.d.ts:79:14 - error TS2583: Cannot find name 'Map'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.

79         map: Map<Type, {
                ~~~

node_modules/@deepkit/type/dist/cjs/src/serializer.d.ts:83:44 - error TS2583: Cannot find name 'Map'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.

83     getStack(registry?: TemplateRegistry): Map<Type, {
                                              ~~~

node_modules/@deepkit/type/dist/cjs/src/serializer.d.ts:201:21 - error TS2583: Cannot find name 'Map'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.

201     classTemplates: Map<ClassType<any>, Template<any>[]>;
                        ~~~

node_modules/@deepkit/type/dist/cjs/src/type-serialization.d.ts:165:11 - error TS2583: Cannot find name 'Map'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later.

165     refs: Map<Type, number>;
              ~~~


Found 18 errors in 9 files.

Errors  Files
     3  node_modules/@deepkit/core/dist/cjs/src/compiler.d.ts:2
       1  node_modules/@deepkit/core/dist/cjs/src/core.d.ts:348
       1  node_modules/@deepkit/core/dist/cjs/src/enum.d.ts:21
       5  node_modules/@deepkit/core/dist/cjs/src/iterators.d.ts:14
       2  node_modules/@deepkit/core/dist/cjs/src/reactive.d.ts:1
       1  node_modules/@deepkit/type/dist/cjs/src/core.d.ts:1
       1  node_modules/@deepkit/type/dist/cjs/src/registry.d.ts:11
       3  node_modules/@deepkit/type/dist/cjs/src/serializer.d.ts:79
       1  node_modules/@deepkit/type/dist/cjs/src/type-serialization.d.ts:165

For some reason it can't find the Map type - and this happens as soon as you import the typeOf function, you don't even have to call it. I tried adding the explicit lib option with ESNext, like it suggests, but that doesn't change anything.

Here's src/hello.ts:

import { typeOf } from '@deepkit/type';

function log(message: string): void {
  console.log(message);
}

//console.log(typeOf(log));

The ts-patch README says "Based on ttypescript - Fully compatible", but yeah, who knows.

Could you give ts-patch a shot and see if it works with your sample project?

All you should need to change is the command named from ttsc to tspc.

@mindplay-dk
Copy link
Author

Hmm, it looks like there's a missing dependency on @types/node ?

It probably should be moved from devDependencies to dependencies ?

"dependencies": {
"@deepkit/type-spec": "^1.0.1-alpha.93",
"@types/uuid": "^8.3.0",
"buffer": "^5.2.1",
"uuid": "^8.3.2"
},
"devDependencies": {
"@deepkit/core": "^1.0.1-alpha.97",
"@deepkit/type-compiler": "^1.0.1-alpha.97",
"@types/lz-string": "^1.3.34",
"@types/node": "14.14.28",
"@typescript/vfs": "^1.3.5",
"lz-string": "^1.4.4"
},

Note that all I did in my test-script is import { typeOf } from '@deepkit/type' - I haven't called any function yet, so this doesn't just happen because I'm trying to reflect a type that depends on Node types.

I did finally get to "hello world" with TS5 + ts-patch though! 😄👍

I see you have Vite support now as well though? That might be better for DX? 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants