ts-node breaks Date reflection mechanism

Created on 14 Jan 2018  路  12Comments  路  Source: TypeStrong/ts-node

import "reflect-metadata";

const CollectMetadata: PropertyDecorator = (target, propertyKey) => {
    const ReflectType = Reflect.getMetadata("design:type", target, propertyKey);
    console.log(ReflectType);
    console.log(ReflectType === Date);
}

class Test {
    @CollectMetadata
    property: Date;
}

ts-node output:

PS E:\#Programowanie\#GitHub\type-graphql> npx ts-node src/date.ts
E:\#Programowanie\#GitHub\type-graphql\node_modules\ts-node\dist\bin.js
[Function: Object]
false

tsc and node output:

PS E:\#Programowanie\#GitHub\type-graphql> npx tsc; node build/date.js
E:\#Programowanie\#GitHub\type-graphql\node_modules\typescript\bin\tsc
[Function: Date]
true

I have no idea what's going on, how can we check the emitted js files?
I'm using ts-node 4.1.0 馃槈

question

Most helpful comment

I would imagine every type information reflection would not work with type information, but perhaps it's only type information from external files? I have not verified this.

I've tested it and every single reflection works (classes from external files, String, Number, Boolean, etc.), only the Date is reflected as Object in this mode. That's why it's weird, should we bump the question to the TS team?

All 12 comments

ping @blakeembrey after a week 馃槣

:+1: I've also encountered this issue

Try using --type-check if you want type information.

Ok, it works but why only Date reflection is broke without this mode? What does --type-check do? 馃槙 Why no "Use TypeScript with type checking" affect on this case?

@blakeembrey why did you close this issue? Does it mean it's not a bug? Will this be covered somehow or is the param workaround the final solution?

What does the --type-check flag do? We're having the same issue in ts-jest.

It does mostly what it sounds like. It uses the TypeScript compiler to compile using the language services and type system information. Without it is only a single file transpile. If you don't have type information, you won't be able to get features that are required by having type information (such as reflecting type information as metadata).

It is not a bug, if you want type information the solution is to enable type information.

Edit: I would imagine every type information reflection would not work with type information, but perhaps it's only type information from external files? I have not verified this.

I would imagine every type information reflection would not work with type information, but perhaps it's only type information from external files? I have not verified this.

I've tested it and every single reflection works (classes from external files, String, Number, Boolean, etc.), only the Date is reflected as Object in this mode. That's why it's weird, should we bump the question to the TS team?

@19majkel94 Without using --type-check? That's really interesting interesting, I expected an all or nothing case since the transpiler mode is a really quick x -> y operation without validation of types. Feel free to ping someone on the TS team, I'm not sure I'll have enough time to investigate the behaviour myself.

I am running in to this problem too. I am using the --type-check flag to work around the issue but that is causing performance issues as sometimes ts-node --type-check .\app.ts takes several minutes to run. For comparison, ts-node .\app.ts takes ~5 seconds to run. I can consistently reproduce the slow compile time by using the --no-cache flag: ts-node --type-check --no-cache .\app.ts.

As @19majkel94 noted, the only type that doesn't get reflected properly when skipping type checking is Date properties. All other types are reflected properly.

I'm not sure how to bring this up with the TS team since tsc always produces the expected output.

Can you please clarify this Issue, since I'm new.

If I'll use ts-node --type-check, terminal will wait for TS input.
But If I need to run unit tests, I need to specify this flag for constant usage.
How is that possible to do?

@An1mus Please check the documentation, it's in the README (you can use environment variables or if you're using by file you can use ts-node/register/type-check). If you need something else, please provide enough information for someone to assist you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dakom picture dakom  路  3Comments

KiaraGrouwstra picture KiaraGrouwstra  路  3Comments

watzon picture watzon  路  3Comments

sodiumjoe picture sodiumjoe  路  4Comments

OliverJAsh picture OliverJAsh  路  3Comments