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

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

Open
aj-bartocci opened this issue Nov 20, 2022 · 17 comments
Labels
bug Something isn't working

Comments

@aj-bartocci
Copy link

aj-bartocci commented Nov 20, 2022

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:

  • Node.js version: 18.10.0
  • TypeScript version: 4.9.3
  • Compiler: ttypescript 1.5.13
  • Bundler: N/A

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.

@aj-bartocci aj-bartocci added bug Something isn't working enhancement New feature or request labels Nov 20, 2022
@Hookyns
Copy link
Owner

Hookyns commented Nov 21, 2022

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.

@Hookyns Hookyns removed the enhancement New feature or request label Nov 21, 2022
@aj-bartocci
Copy link
Author

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

@Hookyns Hookyns changed the title [BUG] "[ERR] tst-reflect: You call getType() method directly..." error when following base usage examples. [BUG] TS 4.9 - "[ERR] tst-reflect: You call getType() method directly..." error when following base usage examples. Nov 21, 2022
@Hookyns Hookyns pinned this issue Nov 21, 2022
@stepansib
Copy link

Any news? It does not work even with TS 4.8.4

@Hookyns
Copy link
Owner

Hookyns commented May 31, 2023

@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.

@Hookyns Hookyns unpinned this issue Aug 8, 2023
@fiftin
Copy link

fiftin commented Aug 18, 2023

Same issue

@Hookyns
Copy link
Owner

Hookyns commented Aug 18, 2023

@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
Copy link

@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
Copy link
Owner

Hookyns commented Oct 25, 2023

@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
Copy link

lewispham commented Oct 26, 2023

@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
Copy link
Owner

Hookyns commented Oct 27, 2023

@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
Copy link

lewispham commented Oct 27, 2023

@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
Copy link
Owner

Hookyns commented Oct 27, 2023

@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
Copy link

@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
Copy link
Owner

Hookyns commented Oct 27, 2023

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
Copy link

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

@Hookyns
Copy link
Owner

Hookyns commented Oct 27, 2023

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants