-
-
Notifications
You must be signed in to change notification settings - Fork 355
fix(types): Object.freeze type pollution from @sentry-internal/replay
#5408
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
Draft
antonis
wants to merge
2
commits into
main
Choose a base branch
from
antonis/Object-freeze-polution
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+140
−1
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /** | ||
| * Type test for Object.freeze pollution fix | ||
| * | ||
| * This test ensures that Object.freeze() resolves to the correct built-in type | ||
| * and not to a polluted type from @sentry-internal/replay. | ||
| * | ||
| * See: https://github.com/getsentry/sentry-react-native/issues/5407 | ||
| */ | ||
|
|
||
| describe('Object.freeze type inference', () => { | ||
| it('should correctly freeze plain objects', () => { | ||
| const frozenObject = Object.freeze({ | ||
| key: 'value', | ||
| num: 42, | ||
| }); | ||
|
|
||
| // Runtime test: should be frozen | ||
| expect(Object.isFrozen(frozenObject)).toBe(true); | ||
|
|
||
| // Type test: TypeScript should infer Readonly<{ key: string; num: number; }> | ||
| expect(frozenObject.key).toBe('value'); | ||
| expect(frozenObject.num).toBe(42); | ||
| }); | ||
|
|
||
| it('should correctly freeze objects with as const', () => { | ||
| const EVENTS = Object.freeze({ | ||
| CLICK: 'click', | ||
| SUBMIT: 'submit', | ||
| } as const); | ||
|
|
||
| // Runtime test: should be frozen | ||
| expect(Object.isFrozen(EVENTS)).toBe(true); | ||
|
|
||
| // Type test: TypeScript should infer literal types | ||
| expect(EVENTS.CLICK).toBe('click'); | ||
| expect(EVENTS.SUBMIT).toBe('submit'); | ||
|
|
||
| // TypeScript should infer: Readonly<{ CLICK: "click"; SUBMIT: "submit"; }> | ||
| const eventType: 'click' | 'submit' = EVENTS.CLICK; | ||
| expect(eventType).toBe('click'); | ||
| }); | ||
|
|
||
| it('should correctly freeze functions', () => { | ||
| const frozenFn = Object.freeze((x: number) => x * 2); | ||
|
|
||
| // Runtime test: should be frozen | ||
| expect(Object.isFrozen(frozenFn)).toBe(true); | ||
|
|
||
| // Type test: function should still be callable | ||
| const result: number = frozenFn(5); | ||
| expect(result).toBe(10); | ||
| }); | ||
|
|
||
| it('should correctly freeze nested objects', () => { | ||
| const ACTIONS = Object.freeze({ | ||
| USER: Object.freeze({ | ||
| LOGIN: 'user:login', | ||
| LOGOUT: 'user:logout', | ||
| } as const), | ||
| ADMIN: Object.freeze({ | ||
| DELETE: 'admin:delete', | ||
| } as const), | ||
| } as const); | ||
|
|
||
| // Runtime test: should be frozen | ||
| expect(Object.isFrozen(ACTIONS)).toBe(true); | ||
| expect(Object.isFrozen(ACTIONS.USER)).toBe(true); | ||
| expect(Object.isFrozen(ACTIONS.ADMIN)).toBe(true); | ||
|
|
||
| // Type test: should preserve nested literal types | ||
| expect(ACTIONS.USER.LOGIN).toBe('user:login'); | ||
| expect(ACTIONS.ADMIN.DELETE).toBe('admin:delete'); | ||
|
|
||
| // TypeScript should infer the correct literal type | ||
| const action: 'user:login' = ACTIONS.USER.LOGIN; | ||
| expect(action).toBe('user:login'); | ||
| }); | ||
|
|
||
| it('should maintain type safety and prevent modifications at compile time', () => { | ||
| const frozen = Object.freeze({ value: 42 }); | ||
|
|
||
| // Runtime: attempting to modify should silently fail (in non-strict mode) | ||
| // or throw (in strict mode) | ||
| expect(() => { | ||
| // @ts-expect-error - TypeScript should prevent this at compile time | ||
| (frozen as any).value = 100; | ||
| }).not.toThrow(); // In non-strict mode, this silently fails | ||
|
|
||
| // Value should remain unchanged | ||
| expect(frozen.value).toBe(42); | ||
| }); | ||
|
|
||
| it('should work with array freeze', () => { | ||
| const frozenArray = Object.freeze([1, 2, 3]); | ||
|
|
||
| // Runtime test | ||
| expect(Object.isFrozen(frozenArray)).toBe(true); | ||
| expect(frozenArray).toEqual([1, 2, 3]); | ||
|
|
||
| // Array methods that don't mutate should still work | ||
| expect(frozenArray.map(x => x * 2)).toEqual([2, 4, 6]); | ||
| expect(frozenArray.filter(x => x > 1)).toEqual([2, 3]); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /** | ||
| * Global type augmentations for the Sentry React Native SDK | ||
| * | ||
| * This file contains global type fixes and augmentations to resolve conflicts | ||
| * with transitive dependencies. | ||
| */ | ||
|
|
||
| /** | ||
| * Fix for Object.freeze type pollution from @sentry-internal/replay | ||
| * | ||
| * Issue: TypeScript incorrectly resolves Object.freeze() to a freeze method | ||
| * from @sentry-internal/replay's CanvasManagerInterface instead of the built-in. | ||
| * | ||
| * See: https://github.com/getsentry/sentry-react-native/issues/5407 | ||
| */ | ||
| declare global { | ||
| interface ObjectConstructor { | ||
| freeze<T>(o: T): Readonly<T>; | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/ban-types -- Matching TypeScript's official Object.freeze signature from lib.es5.d.ts | ||
| freeze<T extends Function>(f: T): T; | ||
|
|
||
| freeze<T extends {[idx: string]: U | null | undefined | object}, U extends string | bigint | number | boolean | symbol>(o: T): Readonly<T>; | ||
|
|
||
| freeze<T>(o: T): Readonly<T>; | ||
| } | ||
| } | ||
|
|
||
| export {}; | ||
|
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test file location at
packages/core/test/typings/object-freeze.test.tsmay not be picked up by Jest'stestPathIgnorePatternsinjest.config.js, which currently ignores<rootDir>/test/typings/directory (note the different directory structure). Verify that this test file is actually executed by the test suite. If the intent is to have type-checking tests ignored from runtime testing, consider either updating the Jest configuration or moving the test to a location that matches the configured patterns.Severity: MEDIUM
🤖 Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID:
3552648