fix: ensure compatibility with object keys minification #4036
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What kind of change does this PR introduce? - Bug fix
Background:
While using advanced JavaScript compilers such as:
The current implementation of the library encounters issues due to direct property access
obj.key
being transformed during the compilation process. This results in runtime errors because object keys get minified or renamed by the compiler.Problem Statement:
In most cases, the current approach works well when defining interfaces in TypeScript. Consider the following example:
Advanced JavaScript compilers like Google Closure Compiler or Terser analyze all usages of this interface and minify keys consistently across all instances:
However, there are certain edge cases where this behavior causes runtime errors.
Root Cause:
The issue occurs when properties are minified but accessed later using a string literal key:
Since the compiler renames
someProperty
todO
, any access using the original keyfoo['someProperty']
will fail because the key no longer exists after minification.A good example of when
defineHiddenProp
orhasOwnProperty
are used:Proposed Solution:
To prevent this, TypeScript’s
declare
keyword should be used when defining interfaces:Using
declare
instructs the compiler not to minify properties in this interface, ensuring compatibility with advanced compilers. This change prevents key renaming and resolves potential runtime issues caused by minification.Please check if the PR fulfills these requirements
npm run build
produced a successful build. (Unit testing can be done by runningnpm test
;)npm run lint
to do this.npm run build
will fail if there are files not linted.)By applying this fix,
ngx-formly
will be more robust, supporting both standard and minified builds without risking property-access issues. Let me know if you need further clarification!