Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

207
Views
Library's types not correctly reflecting in JS file

I am importing a simple JS file via script tag on an HTML page.

As I use jQuery on the website I've added // @ts-check to the top of the JS file, as well as jQuery types:

/**
 * Modal Functionality
 * @param {JQueryStatic} $ Provides jQuery types
 */
const example = ($) => ...

While jQuery types are correctly cast to $, I'm also using the Swiper library and include the library's JS via another script tag. For the sake of argument this could be any other externally imported library.

When using new Swiper('...') in the JS, TS scribbles lines underneath Swiper: Cannot find name 'Swiper'. I've added the following code to an adjacent TS file:

declare global {
    interface Window {
        Swiper: typeof import('swiper'); // swiper types imported from official package
    }
}

let Swiper = window.Swiper;

Going back to my JS file, hovering over Swiper still reads Swiper: any. Shouldn't it reflect the types found from the imported package? Is the JS/TS file structure a problem? How do I get type annotations in a JS file?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Some library include types, some not. You need to install types for Swiper.

npm install --save-dev @types/swiper

After this you can import type in ts file

// index.d.ts
import Swiper from 'swiper';

declare global {
    interface Window {
        Swiper: Swiper;
    }
}

Now I have correctly defined types

[Update 1]

Typescript look global type declaration by default at index.d.ts file. To declare modules or extend window and global properties, you need to declare them in index.d.ts.

How to solve your issue:

  1. Create file index.d.ts in root of project (if you haven`t tsconfig.json file).
  2. Declare Swiper as shown abobe.
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!