How does typescript beautifully define and reference types?

first of all, let"s talk about my problem. I"m currently building a project using webpack + typescript to define a class: NetData.

requirements are as follows:

  • there is a frequently used function: (netdata: NetData) = > void . I wrote an alias using type: type NetDataFunc = (netdata: NetData) = > void; so that when the function is passed as a parameter, the type directly uses NetDataFunc .

then there are many aliases like NetDataFunc . I hope to package them in a file and export them as modules to facilitate global use. Is there a good method

?
Mar.02,2021

< H2 > first of all, let's talk about the reason for yesterday's question: < / H2 >

class NetData is exported by default in NetData, corresponding to NetData.ts, above. This type (NetData) is passed as an argument in other functions, and there are also types like NetDataFunc as arguments to Synchronize.
my concern at the time was that NetData.ts (the default export class NetData, is assumed to be module A), CommonTypeBase.ts (export various basic types for global use, assuming module B). In this way, there will be a situation, A module introduces B module, B module introduces A module.

< H2 > solve < / H2 >

in fact, I didn't change anything in the end. Judging from the result of compilation and packaging, the type declaration in CommonTypeBase.ts will not be packaged

Menu