Skip to content
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

Array.isArray( ) with readonly [] #61381

Open
jonlepage opened this issue Mar 9, 2025 · 2 comments
Open

Array.isArray( ) with readonly [] #61381

jonlepage opened this issue Mar 9, 2025 · 2 comments

Comments

@jonlepage
Copy link

jonlepage commented Mar 9, 2025

🔎 Search Terms

Array.isArray( )

🕗 Version & Regression Information

  • This changed between versions ______ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

Playground

💻 Code

// from: ms-vscode.vscode-typescript-next-5.9.20250307\node_modules\typescript\lib\lib.es5.d.ts
interface ArrayConstructor {
    new (arrayLength?: number): any[];
    new <T>(arrayLength: number): T[];
    new <T>(...items: T[]): T[];
    (arrayLength?: number): any[];
    <T>(arrayLength: number): T[];
    <T>(...items: T[]): T[];
    isArray(arg: any): arg is readonly any[]; // <== fix the Array.isArray() ✅
    isArray(arg: any): arg is any[];
    readonly prototype: any[];
}

//ex: this will work fine now 
	public hasStatus( entityWithStates: EntityWith<C.StatesComponent>, status: STATUS | readonly STATUS[]  ) {
		const { statesMap } = entityWithStates.get( C.StatesComponent ).read;
		return Array.isArray( status ) ? status.some( ( _status ) => statesMap.has( _status ) ) : statesMap.has( status );
	}

🙁 Actual behavior

type became any[]
Image

🙂 Expected behavior

support readonly array when do a Array.isArray()

Additional information about the issue

No response

@jcalz
Copy link
Contributor

jcalz commented Mar 9, 2025

duplicate #17002, also see #55238, #53395, #57566, etc

@jonlepage
Copy link
Author

jonlepage commented Mar 9, 2025

duplicate #17002, also see #55238, #53395, #57566, etc

Thank you friend.
Do you know if, by adding this line in my project, I might encounter problems?
isArray(arg: any): arg is readonly any[];
I have tested it on 3 or 4 scenarios without any issues and it fix my issue, but I assume that if this issue has been running for more than ~8years, it might because it will break the entire ts somewhere?
Playground

However, when I add this line to the Array constructor, the issue is resolved, but I have a slight doubt. Indeed, if a team hasn't added this change, it maybe because it can inevitably lead to more serious issues?
I've learned to be cautious when everything seems too simple to be true.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants