Extending Array prototype throws an "You stumbled upon a bug" error
See original GitHub issueI’m trying to extend Array prototype and import ts file somewhere in my project. Doing so, however, throws the following error:
> dev
> npm run server
> server
> ts-node -r tsconfig-paths/register ./server
Server running at http://localhost:3000
Error: [vite-plugin-ssr@0.3.61][Bug] You stumbled upon a bug in vite-plugin-ssr's source code. Reach out at https://github.com/brillout/vite-plugin-ssr/issues/new or https://discord.com/invite/qTq92FQzKb and include this error stack (the error stack is usually enough to fix the problem). A maintainer will fix the bug (usually under 24 hours). Do not hesitate to reach out as it makes vite-plugin-ssr more robust.
at addString (/Users/alex/Developer/gated-art-web/node_modules/vite-plugin-ssr/dist/cjs/node/html/renderHtml.js:141:28)
at renderTemplate (/Users/alex/Developer/gated-art-web/node_modules/vite-plugin-ssr/dist/cjs/node/html/renderHtml.js:151:9)
at renderHtml (/Users/alex/Developer/gated-art-web/node_modules/vite-plugin-ssr/dist/cjs/node/html/renderHtml.js:37:24)
at executeRenderHook (/Users/alex/Developer/gated-art-web/node_modules/vite-plugin-ssr/dist/cjs/node/renderPage.js:567:58)
at async renderPage (/Users/alex/Developer/gated-art-web/node_modules/vite-plugin-ssr/dist/cjs/node/renderPage.js:89:30)
at async renderPageWithoutThrowing (/Users/alex/Developer/gated-art-web/node_modules/vite-plugin-ssr/dist/cjs/node/renderPage.js:131:16)
at async /Users/alex/Developer/gated-art-web/server/index.ts:42:25
My array extension code is as follows:
export { }
declare global {
interface Array<T> {
compactMap<E>(callback: (t: T) => E | null): Array<E>
}
}
if (!Array.prototype.compactMap) {
Array.prototype.compactMap = function<T, E>(callback: (t: T) => E | null): Array<E> {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return this.map(callback).filter(x => !!x).map(x => x!)
}
}
Also made a bare bones project to reproduce the issue: https://github.com/akolov/vite-ssr-array-prototype-bug
Is there anything I’m doing wrong?
Issue Analytics
- State:
- Created a year ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
[5.10.1] Extending Array.prototype – `Cannot read property ...
Seems like this issue relates to extending Array.prototype . Having such extension is not a must in most project, and even might be...
Read more >JavaScript: What dangers are in extending Array.prototype?
You can add a method to an array, or even an array constructor but you run into issues trying to work with the...
Read more >SSJS Array.prototype throws jintException when used with ...
On a CloudPage the following SSJS code is throwing an error. Code: <script runat="server" language="javascript"> Platform.Load("Core", "1 ...
Read more >Extending JavaScript Natives
Our code gets a useful array extension for free. ... You can't delete or replace a native prototype, but you can edit the...
Read more >How ECMAScript 5 still does not allow to subclass array
Instead of extending Array.prototype , another object would be extended (say, SubArray.prototype ) and then used to initialize (sub)array ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I could reproduce.
This is quite weird indeed. I will dig.
This indeed has worked. Thank you for helping!