1

I'm creating a package in typescript and I generate the declaration files using the tsc tool but when I try to import this package from another project type definitions doesn't load automatically and I get type errors. How can I achieve this problem? I know there are modules that you don't need to install or specify typings to use but I couldn't find how to do this.

EDIT - details: I generate declaration file using the tsc --declarations command and all of them goes directly to the build directory with js files. I've {"types": "./build/index.d.ts", "typescript.tsdk": "./build"} in my package.json but it doesn't seem to effect anything. Here are some examples:

build/index.d.ts

import Network from './network';
import Queue from './queue';
export { Queue, Network, Jobs };

build/network/index.d.ts

import Address from './address';
import Socket from './socket';
import Packet from './packet';
export { Address, Socket, Packet };
declare var _default: {
    Address: typeof Address;
    Socket: typeof Socket;
    Packet: typeof Packet;
};
export default _default;

build/network/packet.dt.ts

export default class Packet {
    private params;
    static fromString(str: string): Packet;
    toString(): string;
}
8
  • please add details, how your d.ts file look, where you put it, how you reference it Commented Feb 18, 2017 at 15:10
  • Alright, I've edited my question and added some examples of declaration files. Commented Feb 18, 2017 at 15:34
  • ok, how do you reference them? what errors do you get? Commented Feb 18, 2017 at 15:45
  • I don't get particularly one error, basically my classes, interfaces etc. don't get defined. So for example I've a class that extends Socket and when I try to use this.connect in it, I get this.connect is not defined error. Commented Feb 18, 2017 at 15:58
  • do you get the error during compilation or runtime? Commented Feb 18, 2017 at 16:01

1 Answer 1

1

Since you use default export and your package is inside node_modules, you can import it the following way:

import Socket from "core/build/network/socket";

class Abc extends Socket {
    constructor() {
        super();

        this.connect('address');
    }
}

let s = new Socket();
s.connect('address');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.