152

I have a Protractor project which contains such a file:

var FriendCard = function (card) {
    var webElement = card;
    var menuButton;
    var serialNumber;

    this.getAsWebElement = function () {
        return webElement;
    };

    this.clickMenuButton = function () {
        menuButton.click();
    };

    this.setSerialNumber = function (numberOfElements) {
        serialNumber = numberOfElements + 1;
        menuButton = element(by.xpath('.//*[@id=\'mCSB_2_container\']/li[' + serialNumber + ']/ng-include/div/div[2]/i'));
    };

    this.deleteFriend = function () {
        element(by.css('[ng-click="deleteFriend(person);"]')).click();
        element(by.css('[ng-click="confirm()"]')).click();
    }
};
module.exports = FriendCard;

Path to the file is ./pages/FriendCard.js.

I have no problems with its import to another file using require():

var FriendCard = require('./../pages/FriendCard');

So, I've decided to import this file to the TypeScript file just like that:

import {FriendCard} from './../pages/FriendCard'

I'm using WebStorm. It tells me that (TS2305) it has no exported member 'FriendCard'.

Maybe I have to configure tsconfig.json file somehow, but I still don't know how it works. Could you help me?

Pang
  • 9,564
  • 146
  • 81
  • 122
SanchelliosProg
  • 2,091
  • 3
  • 20
  • 32
  • 1
    *"I'm using WebStorm, so it tells me, that (TS2305) it has no exported member 'FriendCard'."* Just a small note -- `TS2305` means that the warning/error is produced by actual TypeScript compiler/language service and not actual WebStorm .. as IDE does not use such numbering in their own inspections/parsers. – LazyOne Dec 19 '16 at 11:15

7 Answers7

172

You can import the whole module as follows:

import * as FriendCard from './../pages/FriendCard';

For more details please refer the modules section of Typescript official docs.

Recent Updated Solution : We need to tweak the tsconfig.json to allow JS modules import. credits to @paulmest, @ben-winding @crispen-gari solutions below.

{
  "compilerOptions": {
    "allowJs": true
  }
}
Ram Pasala
  • 4,931
  • 3
  • 16
  • 26
  • 6
    Thanks for articulating "./" in front of the actual relative path. This makes all the difference in path resolution at the compile time. – Roman Oct 06 '17 at 00:19
  • 63
    Is this up to date? I still get the same error... Could not find a declaration file for module ... – Nathan H Feb 10 '19 at 13:08
  • 3
    @nathan-h You probably need to modify the tsconfig file... more info here: https://stackoverflow.com/a/56909179/872328 – PaulMest Dec 13 '20 at 03:45
83

I'm currently taking some legacy codebases and introducing minimal TypeScript changes to see if it helps our team. Depending on how strict you want to be with TypeScript, this may or may not be an option for you.

The most helpful way for us to get started was to extend our tsconfig.json file with this property:

// tsconfig.json excerpt:

{
  ...
  "compilerOptions": {
    ...
    "allowJs": true,
    ...
  }
  ...
}

This change lets our JS files that have JSDoc type hints get compiled. Also our IDEs (JetBrains IDEs and VS Code) can provide code-completion and Intellisense.

References:

PaulMest
  • 12,925
  • 7
  • 53
  • 50
  • 2
    I've put inside **compilerOptions** ``` lang-js { "compilerOptions": { ..., "allowJs": true } ... } ``` – Matheus Abreu Feb 26 '20 at 15:02
  • Same use case like me but imo makes the migration possible harder because you start taking all legacy code as dependency into typescript. – Matthis Kohli Feb 28 '23 at 23:44
16

2021 Solution

If you're still getting this error message:

TS7016: Could not find a declaration file for module './myjsfile'

Then you might need to add the following to tsconfig.json

{
  "compilerOptions": {
    ...
    "allowJs": true,
    "checkJs": false,
    ...
  }
}

This prevents typescript from trying to apply module types to the imported javascript.

Ben Winding
  • 10,208
  • 4
  • 80
  • 67
  • I found this solved my problem if using ts-jest, on a `.ts` file which imports a `.js` file which imports a `.ts` file. A totally unavoidable situation with a clean solution. – Lachlan McDonald Jan 30 '23 at 00:28
15

In your second statement

import {FriendCard} from './../pages/FriendCard'

you are telling typescript to import the FriendCard class from the file './pages/FriendCard'

Your FriendCard file is exporting a variable and that variable is referencing the anonymous function.

You have two options here. If you want to do this in a typed way you can refactor your module to be typed (option 1) or you can import the anonymous function and add a d.ts file. See https://github.com/Microsoft/TypeScript/issues/3019 for more details. about why you need to add the file.

Option 1

Refactor the Friend card js file to be typed.

export class FriendCard {
webElement: any;
menuButton: any;
serialNumber: any;

constructor(card) {
    this.webElement = card;
    this.menuButton;
    this.serialNumber;
}



getAsWebElement = function () {
    return this.webElement;
};

clickMenuButton = function () {
    this.menuButton.click();
};

setSerialNumber = function (numberOfElements) {
    this.serialNumber = numberOfElements + 1;
    this.menuButton = element(by.xpath('.//*[@id=\'mCSB_2_container\']/li[' + serialNumber + ']/ng-include/div/div[2]/i'));
};

deleteFriend = function () {
    element(by.css('[ng-click="deleteFriend(person);"]')).click();
    element(by.css('[ng-click="confirm()"]')).click();
}
};

Option 2

You can import the anonymous function

 import * as FriendCard from module("./FriendCardJs");

There are a few options for a d.ts file definition. This answer seems to be the most complete: How do you produce a .d.ts "typings" definition file from an existing JavaScript library?

Community
  • 1
  • 1
Peter Grainger
  • 4,539
  • 1
  • 18
  • 22
9

I tested 3 methods to do that...

Method1:

      const FriendCard:any = require('./../pages/FriendCard')

Method2:

      import * as FriendCard from './../pages/FriendCard';

Method3:

if you can find something like this in tsconfig.json:

      { "compilerOptions": { ..., "allowJs": true }

then you can write: import FriendCard from './../pages/FriendCard';

sina
  • 2,103
  • 1
  • 19
  • 26
1

apart from adding allowJs and checkJs, like:

{
  "compilerOptions": {
    "allowJs": true,
    "checkJs": false
  }
}

make sure that strict option is not set to true.

erbelion
  • 44
  • 6
0

The answers about adding allowJs: true to your tsconfig.json worked for me, but I also had to make sure the includes: [] block within the tsconfig.json included Javascript files.

{
  "compilerOptions": {
      ...
      "include": ["src/**/*.js"]
      ...
  }
}

Leo
  • 149
  • 4
  • 12