Adamduehansen
Adamduehansen3w ago

Cannot assign number to "number | undefined" when using package.json

Hi. I'm having odd behavior when using a newly created Vite vanilla TypeScript template using deno init --npm vite@latest. Right after initialization I'm installing the game library https://www.npmjs.com/package/excalibur. A game object in the engine is written like this:
import * as ex from "excalibur";

class GameObject extends ex.Actor {
constructor(args: ex.ActorArgs) {
super({
...args,
width: 16,
height: 16
});
}
}
import * as ex from "excalibur";

class GameObject extends ex.Actor {
constructor(args: ex.ActorArgs) {
super({
...args,
width: 16,
height: 16
});
}
}
but the lsp shows error on the width and height properties. It says
Type 'number' is not assignable to type 'undefined'.
though they are both a type of number | undefined. If I replace the package.json file with a deno.json file and reinstalls the node_modules it all works. I have tested on two machines. Is it a bug or am I missing something? Here is how I reproduce: 1. Run deno init --npm vite@latest test-exc, select Vanilla, select TypeScript 2. Run deno install inside the project. 3. Add the Excalibur dependency with deno add npm:excalibur 4. Replace the contents of main.ts with
import * as ex from "excalibur";

class GameObject extends ex.Actor {
constructor(args: ex.ActorArgs) {
super({
...args,
width: 16,
height: 16,
});
}
}
import * as ex from "excalibur";

class GameObject extends ex.Actor {
constructor(args: ex.ActorArgs) {
super({
...args,
width: 16,
height: 16,
});
}
}
5. At this point I have errors on width and height. 6. Delete the package.json and create a deno.json file with this content:
{
"tasks": {
"dev": "deno run -A --node-modules-dir npm:vite",
"build": "deno run -A --node-modules-dir npm:vite build",
"serve": "deno run --allow-net --allow-read https://deno.land/std@0.157.0/http/file_server.ts dist/"
},
"imports": {
"excalibur": "npm:excalibur@^0.29.3",
"vite": "npm:vite@^5.4.8"
},
"compilerOptions": {
"lib": [
"deno.window",
"DOM"
]
},
"lint": {
"exclude": [
"dist"
]
}
}
{
"tasks": {
"dev": "deno run -A --node-modules-dir npm:vite",
"build": "deno run -A --node-modules-dir npm:vite build",
"serve": "deno run --allow-net --allow-read https://deno.land/std@0.157.0/http/file_server.ts dist/"
},
"imports": {
"excalibur": "npm:excalibur@^0.29.3",
"vite": "npm:vite@^5.4.8"
},
"compilerOptions": {
"lib": [
"deno.window",
"DOM"
]
},
"lint": {
"exclude": [
"dist"
]
}
}
6. Fix the import to npm:excalibur, remove the node_modules folder, and reinstall with deno install. 7: Errors are gone.
2 Replies
CodyC
CodyC3w ago
Could they be resolving different versions of the npm:excalibur library? (Check its version in package-lock.json and deno.lock files)
Adamduehansen
AdamduehansenOP3w ago
Oh yeah, I didn't check for different versions. I'll give it another shot. I think they resolved to same version, but now I noticed a warning in the terminal that I might not have payed attention to before:
Warning The following packages contained npm lifecycle scripts (preinstall/install/postinstall) that were not executed: ┠─ npm:core-js@3.36.1 ┃ ┠─ This may cause the packages to not work correctly. ┖─ To run lifecycle scripts, use the --allow-scripts flag with deno install: deno install --allow-scripts=npm:core-js@3.36.1
I ran the command and now the errors are gone :deno_it_works:

Did you find this page helpful?