Enhancing Pixi.js functionality with TypeScript: Utilizing 'none' Module Settings

My project is a 100% JavaScript application that operates in the browser, consisting of around 20 JS files with all function names in the global scope. Whenever I make changes to a JS file, I can simply refresh the page and see the updates immediately.

However, I am interested in incorporating static type checking into my project, which led me to explore TypeScript. By adding type annotations to variables and functions and setting module to none, I was able to seamlessly integrate TypeScript into my workflow. The TypeScript compiler (tsc) performs type checking on the .ts files and outputs the same .js files as before, allowing for hassle-free use in-browser without any modifications.

The issue arises when attempting to utilize a third-party library, such as pixi.js.

When trying to access properties like PIXI.settings.TARGET_FPMS, tsc raises an error stating Cannot find name 'PIXI'. I attempted to address this by including

/// <reference path="pixi.min.js"/>
, but the problem persisted.

According to online resources, the recommended approach involves running npm install pixi.js followed by import * as PIXI from 'pixi.js'. However, opting for this method eliminates the ability to maintain the none module setting. Consequently, I would need to insert import and export statements throughout the codebase, resulting in emitted .js files with incompatible require or import statements geared towards Node.js rather than the browser. This, in turn, necessitates the use of tools like webpack to address these discrepancies, hinting at increased complexity in the project structure.

Is there a way for TypeScript to access the type information from pixi.js without resorting to extensive use of import/export/modules/webpack? Ideally, I aim to leverage tsc for type checking purposes while maintaining the current architecture of the application.

If all else fails, it appears that declaring var PIXI: any; within a file may serve as a viable workaround.

Answer №1

According to a posting on Hacker News:

To demonstrate your use case, here is a basic example: tsconfig.json

{
  "include": ["index.d.ts"],
  "compilerOptions": {
    "target": "es2015",
    "lib": ["es2015", "dom"],
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true
  }
}

index.d.ts

import * as PIXI from 'pixi.js'

declare global {
  interface Window {
    PIXI: typeof PIXI
  }
}

index.ts

console.log(window.PIXI)

An additional import remains, but since it's in the .d.ts file, it won't end up in the runtime code.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error: The selectBox function is not defined for $(...)

Currently, I am encountering an issue with the code snippet below when attempting to select options from a dropdown: Uncaught TypeError: $(...).selectBox is not a function. This error message appears in the console. In order to resolve this problem, I ha ...

Retrieving information from a JavaScript array outputs 'undefined'

I am currently attempting to retrieve the attribute labeled "MediaURL" from my Javascript array object. To provide a clearer understanding, the image below illustrates an expanded view of the array: https://i.sstatic.net/BWNym.png Encountering the follow ...

Learn the process of transferring a complete JSON object into another object using Angular

I'm having trouble inserting one object into another. My JSON object is multidimensional, like this: { "item1":{ "key":"Value", "key":"Value" }, "item2":{ "key ...

Creating an array like this in JavaScript during an API call using Ajax

"WorkingHours": { "Monday" : { "open" : "10:00 am", "close" :"5:00 pm" }, "Wednesday" : { "open" : "10:00 am", "close" :"5:00 pm" ...

Guide to accessing JSON APIs by utilizing a link within another API that contains JSON data linking to yet another JSON file

Searching for a Solution to Fetch Data from a Specific URL and Display it on a New Screen I have successfully called the API using the following link: - Completed After receiving JSON data, I was able to populate my DataTable. - Completed Upon clicking a ...

Name or Title of a Polygon/Polyhedron Using Three.js

My page contains a sample code that successfully retrieves the name of an object when a user clicks on it. However, the code works well with cubes and spheres but fails with polygons. To see how the clicks respond, you can check the console logs. What shou ...

What is the procedure for invoking the delete route via ajax in Laravel?

When attempting to call a route resource with AJAX using the 'DELETE' method, an error is encountered (Exception: "Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException") along with the message "The DELETE metho ...

What techniques can I use to adjust the size of an image through zooming in and out?

In my custom gallery component, the crucial code section looks like this: <Gallery> <Header> <img src={galleryIcon} alt='Galley icon' /> <h1>My Gallery</h1> </Header> ...

What steps can I take to improve my routing structure in nodejs and express?

My current WebAPI code is pretty modular, but I want to make it even more so. Right now, all the routes are in my server.js file and I would like to separate them into individual controllers. Any suggestions on how to achieve that? Here's an example: ...

Mocking Ext.Ajax.request in ExtJS 4.2.1 is a process of em

When it comes to frontend unit testing using Jasmine, one of the challenges I faced was mocking all the requests in my application. Luckily, I have already tackled a method to mock all my proxies successfully: proxy: appname.classes.proxy.ProxyNegotiator ...

Having difficulty configuring particlesJS as the background

If you're looking to create a three-page single scrolling webpage with particlesJS as the background, consider using the following resources: particlesJS Website and particlesJS Github Page. An issue arises when trying to set particlesJS as the back ...

When I changed the encoding of a live texture to sRGB in Three.js, the frame rate dropped by fifty percent

I am currently working on a threejs application that requires updating a texture in each frame. The output encoding of the THREE.WebGLRenderer is set to sRGB. Upon setting the texture encoding to sRGB, I observed that the rendering result is accurate. How ...

Setting up Npm Sequelize Package Installation

Could use some help with setting up Sequelize. Here's the current status: https://i.sstatic.net/MQH1I.jpg ...

New Solution for Direct Java Raw Printing in Chrome Browser Post NPAPI Discontinuation

It's common knowledge that NPAPI will soon be eliminated from Chrome. Is there a substitute for the Jzebra/QZ Java plugin that allows for raw printing (sending raw ESC/P commands) to POS printers? Are there any Chrome APIs (based on HTML5 and Javasc ...

The Chrome Extension Javascript popup appears to be empty

Feeling a bit lost on my first attempt at creating a chrome extension. Check out my manifest.json below: { "manifest_version": 2, "name": "Get Offensive Wallpapers", "version": "1.0", "permissions": [ "tabs", "http://*/*", "https://*/*" ], ...

data not corresponding to interface

I am encountering an issue that says Type '({ name: string; href: string; icon: IconDefinition; } | { name: string; href: string; icon: IconDefinition; childs: { name: string; href: string; icon: IconDefinition; }[]; })[]' is missing the followin ...

Modify the appearance of selectInput and numericInput controls

Looking to update the appearance of my Shiny UI elements to better match the overall website design. Specifically, I want to eliminate the rounded edges on the selectInput boxes for a cleaner look like this: https://i.sstatic.net/pepak.png Additionally, ...

Troubleshooting Problem with JQuery Datepicker Input Field Value

There are two text boxes on my page named fromDate and toDate for date search. By default, the current day is displayed in both of them. Here is the working code: Jquery (document).ready(function () { $('.dateClass').datetimepicker({timepi ...

Can someone provide guidance on how to send serialized data using a jQuery.getScript() request?

Is it feasible to make a request for an external JS file while simultaneously sending serialized data in that same request? I want to provide some values to validate the request, but without including those values in the request URL. Upon receiving the po ...

Click to refresh a different component in ReactJS

My goal is to create a unique media player that can reload when a user wants to listen to an MP3 file. The concept is as follows: In media-player.js: - Display title, artist, and album art In programs.js: there is a button (LISTEN) that renders in medi ...