Hookyns / tst-reflect

Advanced TypeScript runtime reflection system
MIT License
338 stars 11 forks source link

[BUG] TS 4.9 - "[ERR] tst-reflect: You call getType() method directly..." error when following base usage examples. #84

Open aj-bartocci opened 1 year ago

aj-bartocci commented 1 year ago

Describe the bug When following the README examples for using getType I continuously get the error [ERR] tst-reflect: You call getType() method directly. You have probably wrong configuration, because tst-reflect-transformer package should replace this call by the Type instance.

I decided to clone the whole repo and to run the examples to see if maybe it was something with my configuration but I get the same issue when trying to run the base-example.

The code causing the problem

// index.ts
import { getType } from "tst-reflect";

interface ITest {
    foo: string
    bar?: string
    bazz: number
}

console.log(getType<ITest>());

// package.json
{
  "name": "example",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "npx ttsc & node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "tst-reflect": "^0.12.0"
  },
  "devDependencies": {
    "tst-reflect-transformer": "^0.12.5",
    "ttypescript": "^1.5.13"
  }
}

// tsconfig.json
{
    "compilerOptions": {
        "module": "commonjs",
        "target": "ES2020",
        "removeComments": true,
        "sourceMap": true,
        "experimentalDecorators": true,
        "rootDir": "./",
        "plugins": [
            {
                "transform": "tst-reflect-transformer"
            }
        ]
    },
    "reflection": {
        "metadata": false,
        "debugMode": false
    }
}

To Reproduce Steps to reproduce the behavior:

  1. npm i
  2. npm run start
  3. see error...

Expected behavior I expect that the type information would print out and not get an error.

Runtime:

Additional context I'm checking out this library because I would like to use it with an express server, if that is not possible please let me know.

Hookyns commented 1 year ago

Hi, we discovered this last week.

It's caused by TS 4.9, they changed something in the transformer API.

I'm going to debug it this week. I hope it's just a minor change... Until then you have to downgrade to TS 4.8.x.

aj-bartocci commented 1 year ago

awesome thanks for the quick response, I was able to get it working with 4.8

stepansib commented 1 year ago

Any news? It does not work even with TS 4.8.4

Hookyns commented 1 year ago

@stepansib This is working example with TS v4.8.4: https://stackblitz.com/edit/tst-reflect-example?file=index.ts I think your issue is not related to TS version. Please, create new issue and fill in all required information.

fiftin commented 1 year ago

Same issue

Hookyns commented 1 year ago

@fiftin It seems to work. Check this out https://stackblitz.com/edit/tst-reflect-example-ts49?file=index.ts

If you are not able to make it work, please create a repro on StackBlitz and I'll take a look what the issue is. 🙂

lewispham commented 11 months ago

@Hookyns Your link is broken. It looks like a ttypescript issue. I tried with ts-patch but this error still happens. Could you consider supporting ts-patch instead of ttypescript?

Hookyns commented 11 months ago

@Hookyns Your link is broken. It looks like a ttypescript issue. I tried with ts-patch but this error still happens. Could you consider supporting ts-patch instead of ttypescript?

@lewispham You mean example is broken, not the link, right? Both links seems OK. The 4.8.4 example had some issue with dependencies, I fixed that.

You can use ts-patch for sure. As far as I know ts-patch is compatible with ttypescript out of the box.

lewispham commented 11 months ago

@Hookyns Yes the example. I've just changed the typescript version to 5.2.2 then it's broken. It looks like a ttypescript issue. Then I switch to use ts-patch and having this error.

[ERR] tst-reflect: You call getType() method directly. You have probably wrong configuration, because tst-reflect-transformer package should replace this call by the Type instance.
If you have right configuration it may be BUG so try to create an issue.
If it is not an issue and you don't want to see this debug message, create field 'tst-reflect-disable' in global object (window | global | globalThis) eg. `window['tst-reflect-disable'] = true;`

Here is my package.json in your example.

{
  "name": "node-starter",
  "version": "0.0.0",
  "scripts": {
    "start": "npx tspc && node index.js",
    "test": "echo \"Error: no test specified\" && exit 1",
    "prepare": "ts-patch install -s"
  },
  "dependencies": {
    "tst-reflect": "^0.12.0"
  },
  "devDependencies": {
    "tst-reflect-transformer": "^0.12.0",
    "ts-patch": "^3.0.2",
    "typescript": "^5.2.2"
  }
}

URL: https://stackblitz.com/edit/tst-reflect-example-ts522-s92ole So neither ttypescript or ts-patch work with typescript version 5.2.2.

Hookyns commented 11 months ago

@lewispham I'll take a look, but it's probably issue with the ts-patch. Maybe because typescript changed some API again.

In case you are willing to help with this you can try to add at least some console.log() to node_modules/tst-reflect-transformer/dist/index.js to verify if it gets called at all.

lewispham commented 11 months ago

@Hookyns It's only called when I run tspc without importing tst-reflect. It's not called when I added this to my code.

import { getType, Type } from "tst-reflect";
const type: Type = getType();
console.log("TYPE", type);
Hookyns commented 11 months ago

@Hookyns It's only called when I run tspc without importing tst-reflect. It's not called when I added this to my code.

It should be called each time you run tspc. It should not depend on anything inside your typescript being transpiled. 🤔

EDIT: nvm; I didn't noticed there are logs:

[INF] tst-reflect-transformer: using tsconfig '/home/projects/tst-reflect-example-ts522-s92ole-pu6wap/tsconfig.json'.
[INF] tst-reflect-transformer: detected root directory '/home/projects/tst-reflect-example-ts522-s92ole-pu6wap'.

so it is called... 🤔

lewispham commented 11 months ago

@Hookyns I meant that when I added console.log into node_modules/tst-reflect-transformer/dist/index.js then run tspc, the console.log is not called when I added the above code to my code base but it's called when I remove that code from my code base. In other words, it looks like ts-patch does not use tst-reflect-transformer for transforming the code when I add those 3 lines into my code base.

Hookyns commented 11 months ago

I think I found it. tst-reflect-transformer depends on TS 4.x, but when you use TS 5.x, ts-patch is initialized using the TS 5; source code is parsed by TS 5 and it's incompatible with the 4.9 transformer API so each file is skipped because of my check (in index.js)

if (!ts.isSourceFile(node)) {
    return node;
}

because node is from TS 5 but the ts.isSourceFile is from TS 4.9 and it returns false for that TS 5 node.

lewispham commented 11 months ago

Glad to hear that. Would removing the check fix the issue?

Hookyns commented 11 months ago

Nope. tweaked demo It ends with some error.

So I would have to update typescript to make it work but there were quite a lot of changes in the typescript API. It's something I don't want to do in this old ts-reflect version. If it is option for you, join the closed alpha and try the new system. See #78. New system with typegen is working with esbuild. TypeScript nor SWC based builds are not supported yet, but just because I didn't create packages for that; it's possible and maybe easier than updating TypeScript in this old tst-reflect.

PS: New system is not yet as stable as this old tst-reflect version.

lewispham commented 11 months ago

OK I see. Please add me to that project. Thank you.

GulgDev commented 1 month ago

Same issue, typescript 4.8.3

Hookyns commented 1 month ago

Same issue, typescript 4.8.3

Hello @GulgDev, That must be something in your configuration IMHO. It means that the transformer did not transformed the code, though.

This is working 4.8 demo: StackBlitz

hosam95 commented 4 weeks ago

having the same issue here with typescript@5.5.4

it works fine when i use ts-node@10.9.2 with ts-patch@3.2.1 would really appreciate some explanation of how/why it works

Hookyns commented 4 weeks ago

having the same issue here with typescript@5.5.4

it works fine when i use ts-node@10.9.2 with ts-patch@3.2.1 would really appreciate some explanation of how/why it works

Hi @hosam95, TS 5.x is not supported. Explained here, few comments above.

PS: You may be interested in #78 All TS versions are supported with the new system. Even esbuild, swc, Vite etc. It's not finished yet but maybe a thing you need may be implemented already.