We are creating an npm module from .net entities. But I'm not able to use it on my angular2 component.
That's the structure module: enter image description here
I want to import from index.d.ts (into npm) we decide to create a module in order to satisfy the types referenced
declare module cio {
export class Address {
addressLine1: string;
addressLine2: string;
city: string;
state: string;
country: string;
postalCode: string;
}
. . .
declare module cio {
export class Business {
id: string;
typeName: string;
createdDate: Date;
lastModifiedDate: Date;
createdBy: string;
lastModifiedBy: string;
isTest: boolean;
isDeleted: boolean;
taxId: string;
businessType: BusinessType;
businessName: string;
address: Address;
phone: string;
mobile: string;
fax: string;
email: string;
}
enum BusinessType {
Individual = 1,
Company = 2,
}
}
I tried to import using
import { Address, ... } from 'npm_module_name/index';
and create an object like
let testAddress : Address = new Address();
Error:(31, 16) TS2304:Cannot find name 'Address'.
I tried to import using
import { cio } from 'npm_module_name/index/';
and create an object like
let testAddress : cio.Address = new cio.Address();
Error:(31, 20) TS2694:Namespace ''*'' has no exported member 'Address'.
I tried to replace module by namespace but didn't work
How is the best way to import on my component? Thanks
I recently had the need to do this, and here is what I have done.
First of all, make sure you are using the correct TypeScript configuration (
tsconfig.json
) for npm package; most important stuffs are shown below:Next, the idea is to use the npm package name as module. So you can do something like following while using this package.
The name of the npm module comes naturally from you
package.json
file. There you can also mention your main file in the distribution as well as the location of your type definitions. An example is shown below (this does not include other information such as licences, repository etc. for brevity).I chose to generate the
./index.ts
in my build step (using gulp) that just exports everything from my package, and for this I have usedgulp-create-tsindex
. Using this you can generate aindex.ts
which looks like follows:Note that
gulp-create-tsindex
flattens the folder structure. Thus, even ifstuffs
is indir1/subdir2/somestuffs.ts
, you still need toimport
it using the above mentionedimport
syntax (import {stuffs} from "YourAwesomePackage"
).Using this a minimal build task looks as follows:
Hope this helps.