class-transformer: fix: builds fail with Angular 5 and Angular CLI when @Type is used
After upgrading to Angular 5 and Angular CLI 1.5.0, my production builds stopped working. It took me quite some time to figure out, that using the @Type decorator from class-transformer lead to the following error message when running ng build -prod:
ERROR in Error: TypeError: Cannot read property 'kind' of undefined
at nodeCanBeDecorated (/demo/angular5-ct/node_modules/typescript/lib/typescript.js:7805:35)
at nodeIsDecorated (/demo/angular5-ct/node_modules/typescript/lib/typescript.js:7825:16)
at nodeOrChildIsDecorated (/demo/angular5-ct/node_modules/typescript/lib/typescript.js:7829:16)
at Object.forEach (/demo/angular5-ct/node_modules/typescript/lib/typescript.js:1506:30)
at Object.childIsDecorated (/demo/angular5-ct/node_modules/typescript/lib/typescript.js:7835:27)
at getClassFacts (/demo/angular5-ct/node_modules/typescript/lib/typescript.js:51088:20)
at visitClassDeclaration (/demo/angular5-ct/node_modules/typescript/lib/typescript.js:51113:25)
at visitTypeScript (/demo/angular5-ct/node_modules/typescript/lib/typescript.js:50972:28)
at visitorWorker (/demo/angular5-ct/node_modules/typescript/lib/typescript.js:50785:24)
at sourceElementVisitorWorker (/demo/angular5-ct/node_modules/typescript/lib/typescript.js:50817:28)
at saveStateAndInvoke (/demo/angular5-ct/node_modules/typescript/lib/typescript.js:50738:27)
at sourceElementVisitor (/demo/angular5-ct/node_modules/typescript/lib/typescript.js:50799:20)
at visitNodes (/demo/angular5-ct/node_modules/typescript/lib/typescript.js:49280:48)
at Object.visitLexicalEnvironment (/demo/angular5-ct/node_modules/typescript/lib/typescript.js:49313:22)
at visitSourceFile (/demo/angular5-ct/node_modules/typescript/lib/typescript.js:51059:53)
at saveStateAndInvoke (/demo/angular5-ct/node_modules/typescript/lib/typescript.js:50738:27)
As soon as I removed @Type, the build was working fine again. I have no idea where this has to be fixed, but hoped that maybe someone here knows what I can do to be able to use the latest Angular version with class-transformer.
I have created a simple demo project here: https://github.com/philippd/angular5-ct
Just run ng build -prod to see the error.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 16
- Comments: 30 (3 by maintainers)
@sandcake Can be made more universal, I came to this solution (this works on angular 5):
From the workaround mentioned in angular/angular#20216 For now using:
instead of :
works
We need to wait until angular team fix this issue. For now solution to avoid this problem is following:
Workaround is available https://github.com/angular/angular/issues/20216
I want to share my workaround that I put together after reading other advices here and there. It’s patching an
ngscript from@angular/cli@1.6.4(current version at the moment of writing). Because of that it prevents updating@angular/cli, so be prepared.ngsomewhere in your repo. You can grab patched version from here: https://github.com/kirillgroshkov/angular-cli/blob/18f14d71bc5d73dba488f8fc1d08fd46d1d885f1/packages/%40angular/cli/bin/ngHere’s what changed, compared to the original file: https://github.com/kirillgroshkov/angular-cli/commit/18f14d71bc5d73dba488f8fc1d08fd46d1d885f1
postinstallscript in yourpackage.jsonthat will run every time after you donpm install(oryarn install) and copy/overwritengfrom your location tonode_modules/@angular/cli/bin/ng.I resolved upgrading typescript to 2.7, throwing away
forwardRef, and adding// @dynamicat the top of the class:https://github.com/angular/angular-cli/issues/8434
@gmavritsakis
I had the same issue with: Angular CLI 1.6.1 Typescript 2.4.2 Angular 5.0.2
Found a solution by changing typescript.js for now. Replace all the function function nodeCanBeDecorated(node) with the following code
Which comes from here, if you compile typescript: shlomiassaf/TypeScript@7017fa2
Hi, same problem here, and thanks for the workaround, it works! want to know if there’s gonna be a fix in next releases?
Also problem remain
Oh, I same problem )) @Type(() => …) I forgot about them
Pass the function reference, don’t execute the function.