简体   繁体   中英

Is there a way to use a npm package without d.ts definition on TypeScript

Is there a way to use a npm package without d.ts definition on TypeScript and node js? And if yes, how can i achieve that?

Currently, I have something like this, and it throws an error, that tell me "cannot find module 'node-rest-client'", and is because .d.ts file not exists.

import * as NodeRest from 'node-rest-client';

export class MyClass {
}

EDIT I solved like this:

/// <amd-dependency path="node-rest-client" />
let NodeClient = require('node-rest-client');
export class MyClass { }

It's good this solution, or is better implements Dan's response?

I would reconsider adding the types, at least on it's basic form.

you'll be missing the main reason to use typescript

but, sometimes is not convenient, or you want to stay focus on the current task.

in that case, when in "Node" or using a bundler ( webpack, ...etc)

you can always resort to "require"

greeter.js

module.exports = {
    greet: () => "hello"
}

greeter.test.ts

import * as assert from "assert";
interface IGreeter {
    greet(): string;
}
const greeter: IGreeter = require("./greeter");
describe("amodule", () => {
    it("works", () => {
        assert.equal(greeter.greet(), "hello");
    });
});

or simply (noImplicitAny: false)

require("./greeter").greet()

or (noImplicitAny: true)

(require("greeter") as any).greet()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM