Repository navigation
Closed
Labels
area: @angular-devkit/build-angularfreq1: lowOnly reported by a handful of users who observe it rarelyseverity5: regressiontype: bug/fix
Description
Which @angular/* package(s) are the source of the bug?
compiler-cli, compiler, core
Is this a regression?
No
Description
Hi! I'm updating to Angular 16 and I've found a significally difference with the resulted compiled code between typescript 4.9.4 and 5.0.4
Here is an example of the problematic code:
export class IssueService {
#throwIfUndefined = this.assertIfPropertyIsDefined();
constructor(private readonly property) {}
assertIfPropertyIsDefined() {
if (!this.property) {
throw new Error('the property should be defined');
}
}
}Using Typescript 4.9.4 the service is transpiled to:
__webpack_require__.r(__webpack_exports__);
/* harmony export */
__webpack_require__.d(__webpack_exports__, {
/* harmony export */
"IssueService": ()=>(/* binding */
IssueService)/* harmony export */
});
/* harmony import */
var _xxx_node_modules_babel_runtime_helpers_esm_classPrivateFieldInitSpec_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./node_modules/@babel/runtime/helpers/esm/classPrivateFieldInitSpec.js */
4482);
/* harmony import */
var _xxx_node_modules_babel_runtime_helpers_esm_classPrivateFieldSet_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./node_modules/@babel/runtime/helpers/esm/classPrivateFieldSet.js */
692);
var _throwIfUndefined = /*#__PURE__*/
new WeakMap();
class IssueService {
// Get attention to these lines
constructor(property) {
(0,
_xxx_node_modules_babel_runtime_helpers_esm_classPrivateFieldInitSpec_js__WEBPACK_IMPORTED_MODULE_0__["default"])(this, _throwIfUndefined, {
writable: true,
value: void 0
});
this.property = property;
(0,
_xxx_node_modules_babel_runtime_helpers_esm_classPrivateFieldSet_js__WEBPACK_IMPORTED_MODULE_1__["default"])(this, _throwIfUndefined, this.assertIfPropertyIsDefined());
}
assertIfPropertyIsDefined() {
if (!this.property) {
throw new Error('the property should be defined');
}
}
}But using Typescript 5.0.4 the service is transpiled to:
__webpack_require__.r(__webpack_exports__);
/* harmony export */
__webpack_require__.d(__webpack_exports__, {
/* harmony export */
"IssueService": ()=>(/* binding */
IssueService)/* harmony export */
});
/* harmony import */
var xxx_node_modules_babel_runtime_helpers_esm_classPrivateFieldInitSpec_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./node_modules/@babel/runtime/helpers/esm/classPrivateFieldInitSpec.js */
4482);
var _throwIfUndefined = /*#__PURE__*/
new WeakMap();
class IssueService {
constructor(property) {
// The code is different and this code produces an exception where using 4.9.4 don't
(0,
_xxx_node_modules_babel_runtime_helpers_esm_classPrivateFieldInitSpec_js__WEBPACK_IMPORTED_MODULE_0__["default"])(this, _throwIfUndefined, {
writable: true,
value: this.assertIfPropertyIsDefined()
});
this.property = property;
}
assertIfPropertyIsDefined() {
if (!this.property) {
throw new Error('the property should be defined');
}
}
}As you can see, the difference comes from the initialization of private fields. Well, I can't reproduce it on typescript side
Maybe it's a babel issue, but I don't know how to check it and I haven't found any issue about it...
I've prepared a minimal reproduction:
- this commit works: tonivj5/angular-ts-5-transpilation-issue@8b995e3 (ts 4.9.4, good transpilation)
- this commit doesn't work: tonivj5/angular-ts-5-transpilation-issue@6cd8013 (ts 5.0.4, bad transpilation)
Please provide a link to a minimal reproduction of the bug
Please provide the exception or error you saw
Ucaught Error: the property should be defined
at IssueService.assertIfPropertyIsDefined (issue.service.ts:8:13)
at new IssueService (issue.service.ts:2:28)
at 6747 (app.module.ts:13:17)
at __webpack_require__ (bootstrap:19:1)
at 4431 (issue.service.ts:10:3)
at __webpack_require__ (bootstrap:19:1)
at __webpack_exec__ (main.ts:7:36)
at main.ts:7:36
at __webpack_require__.O (chunk loaded:23:1)
at main.ts:7:36
a
Please provide the environment you discovered this bug in (run ng version)
Node.js version v19.4.0 detected.
Odd numbered Node.js versions will not enter LTS status and should not be used for production. For more information, please see https://nodejs.org/en/about/releases/.
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
Angular CLI: 16.0.0
Node: 19.4.0 (Unsupported)
Package Manager: npm 9.2.0
OS: linux x64
Angular: 16.0.0
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1600.0
@angular-devkit/build-angular 16.0.0
@angular-devkit/core 16.0.0
@angular-devkit/schematics 16.0.0
@schematics/angular 16.0.0
rxjs 7.8.1
typescript 5.0.4
Warning: The current version of Node (19.4.0) is not supported by Angular.
Anything else?
No response
Reacted by Carlo Bertoldi González, Basilio, David Rojo and jsegado-mgd
Reacted by jsegado-mgd
Metadata
Metadata
Assignees
Labels
area: @angular-devkit/build-angularfreq1: lowOnly reported by a handful of users who observe it rarelyseverity5: regressiontype: bug/fix
Activity
[-]Angular produces incorrect transpiled code using typescript `5.0.4` but correct one using `4.9.4`[/-][+]Angular produces incorrect transpiled code using typescript `5.0.4` but correct one using `4.9.x`[/+]JeanMeche commentedon May 5, 2023
Hi Toni,
This is a typescript issue. With
ES2022and Ts5.0, properties are init before the constructor.See this playground and the generated js code on the right.
tonivj5 commentedon May 5, 2023
Thanks for the fast response!
I know this behavior is expected when

useDefineForClassFieldsis enabled, but I think it's unexpected ifuseDefineForClassFieldsisfalse. Angular disables this property by default as you can see in the reproduction repoSo, using
4.9.xor5.0.4both have same options and both use babel to downgrade private class fields (as I can infer from generated code)Revert "fix(@angular-devkit/build-angular): set public class fields a…
Revert "fix(@angular-devkit/build-angular): set public class fields a…
Revert "fix(@angular-devkit/build-angular): set public class fields a…
tonivj5 commentedon May 11, 2023
Hi again! @alan-agius4 I have updated the repro to the latest version (
16.0.1) and the error persists 😿Commit updating to latest: tonivj5/angular-ts-5-transpilation-issue@8bd0584
alan-agius4 commentedon May 11, 2023
Looks like there is a TypeScript bug when using ECMA private fields.
See: microsoft/TypeScript#54206
tonivj5 commentedon May 11, 2023
Thanks for pointing me to that issue! I tried to reproduce it on typescript side but I wasn't able of do it... 😓
Then, the PR #25162 is correct or not? Seeing it's a typescript issue....
angular-automatic-lock-bot commentedon Jun 11, 2023
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.