How to create typescript project for export

I apologize for being new to the js ecosystem. I work in a place where typescript is used, which is great because it helped me move from the background of static languages. However, I still do not understand the packaging / module system, especially when combined with typescript definitions.

I am writing a module with a bunch of interfaces describing the web service API contracts that will be used by the client library and the API service itself. If it was C # / Java or something, I would write my interface files, pack it, publish it, and it will be consumed by saying

import GetWizardsResponse from StuffInc.Contract.Wizards

This is how I approached my typescript library, so I wrote

export interface GetWizardsResponse {
    wizards: Wizard
    currentPage: number
    count: number
}

Then in mine ts.configI have "declaration": true, so after tscI get a Wizards.d.tsand Wizards.js. When I npm publish, I should use it, doing something like this:

import {GetWizardsResponse} from '@StuffInc/contracts/Wizards'

but when I look around, this is more common in such imports:

import * as Express from 'express'

or

import { Response, NextFunction } from 'express'

so I take it using my package should look like this:

import { GetWizardsResponse } from '@StuffInc/contracts

Thus, I have /wizards, /goblins, /elves, but the import always comes from the `from @ StuffInc / contract '. What am I, what (what should I do right? All NPM packages look like this)

To do this, I realized that I needed to put everything in one file index.d.ts. Can I write my code in logical separate files, but embed it in one definition so that other developers will not guess?

+4
source share
1

, , , - index.ts export * from '@StuffInc/contracts/Wizards'

, - . , webpack, , index.d.ts .d.ts, ( .d.ts).

js/ts, es6, , . , , import package from 'package'. , Moment, , , . import moment, { Duration } from 'moment'. , , . import * as moment from 'moment'. , /default. , * , , node, , , .

, , .

0

Source: https://habr.com/ru/post/1692076/


All Articles