简体   繁体   English

Typescript:es6 从 node_modules 子文件夹导入类型定义 (.d.ts)

[英]Typescript: es6 import type definition (.d.ts) from node_modules subfolder

I have a npm package that has the following type definitions (simplified):我有一个 npm package 具有以下类型定义(简化):

./node_modules/ag-grid-react/main.d.ts ./node_modules/ag-grid-react/main.d.ts

export declare class AgGridReact extends Component<AgGridReactProps, {}> {}

./node_modules/ag-grid-react/lib/agGridReact.d.ts ./node_modules/ag-grid-react/lib/agGridReact.d.ts

export declare class AgGridReact extends Component<AgGridReactProps, {}> {
    gridOptions: GridOptions;
    api: GridApi | null;
    columnApi: ColumnApi;
}

I am using the component in my react component like this:我在我的反应组件中使用该组件,如下所示:

import { AgGridReact } from 'ag-grid-react'

const HelpRequests= () => {
  const grid = useRef<AgGridReact>(null)

  if (grid.current) console.log(grid.current.columnApi)

  return (<AgGridReact ref={grid}/>)
}

The Problem:问题:

Typescript does complain that there is no columnApi. Typescript 确实抱怨没有 columnApi。 It seems it sadly picks the wrong type from the main.d.ts可悲的是,它似乎从 main.d.ts 中选择了错误的类型

I found that I can import the type from the agGridReact.d.ts directly and use it like this:我发现我可以直接从 agGridReact.d.ts 导入类型并像这样使用它:

import {AgGridReact as AgGridReactType} from 'ag-grid-react/lib/agGridReact'

...

const grid = useRef<AgGridReactType>(null)

Question:问题:

Is this the correct way to address this issue?这是解决此问题的正确方法吗? Will typescript be smart enough not to import the ./node_modules/ag-grid-react/lib/agGridReact.ts file which could cause my bundle size to go up? typescript 是否足够聪明,不会导入./node_modules/ag-grid-react/lib/agGridReact.ts文件,这可能会导致我的包大小增加到 go ?

I've searched a lot but could not find anything about importing types only from node_modules subfolders.我进行了很多搜索,但找不到有关仅从 node_modules 子文件夹导入类型的任何信息。

I will try to answer this:我将尝试回答这个问题:

Let's assume there is an xyz library and it has these files:假设有一个xyz库,它有以下文件:

xyz/lib/main.ts: xyz/lib/main.ts:

export const test = 1000

and

xyz/main.ts: xyz/main.ts:

export * from './lib/main.ts'
export const test = 'foo bar'

And I would like to use xyz in my app.ts , and I am aware of only its main.ts file as I think it is the main file which exports everything from library.我想在我的app.ts中使用xyz ,我只知道它的main.ts文件,因为我认为它是从库中导出所有内容的主文件。 So I am most likely to do:所以我最有可能这样做:

app.ts:应用程序.ts:

import { test } from './xyz/main'

console.debug(test) // it will print 'foo bar'

Now, somebody goes and comment this line in the library:现在,有人在库中评论这一行:

xyz/main.ts: xyz/main.ts:

export * from './lib/main.ts'
// export const test = 'foo bar'

Now, what will be printed by my app.ts ?现在,我的app.ts将打印什么? It will print 1000 .它将打印1000


The same thing is happening there with ag-grid-react .同样的事情也发生在ag-grid-react那里。 It ( ag-grid-react/main.d.ts ) is overriding the apparently correct (better) class declaration present in ag-grid-react/lib/agGridReact.d.ts .它( ag-grid-react/main.d.ts覆盖ag-grid-react/lib/agGridReact.d.ts中明显正确(更好)的 class 声明。 And it is perfectly fine to import from inner path .内部路径导入是完全可以的。

main.d.ts: main.d.ts:

export * from './lib/agGridReact'; // it is exporting from innner path too
export declare class AgGridColumn extends Component<AgGridColumnProps | AgGridColumnGroupProps, {}> { // and overriding here at the same time
}

agGridReact.d.ts: agGridReact.d.ts:

export declare class AgGridReact extends Component<AgGridReactProps, {}> {
    props: any;
    state: any;
    static propTypes: any;
    gridOptions: GridOptions;
    changeDetectionService: ChangeDetectionService;
    api: GridApi | null;
    columnApi: ColumnApi;
    portals: ReactPortal[];
    hasPendingPortalUpdate: boolean;
    destroyed: boolean;
    protected eGridDiv: HTMLElement;
    private static MAX_COMPONENT_CREATION_TIME;
    constructor(props: any, state: any);
    render(): React.ReactElement<any, string | ((props: any) => React.ReactElement<any, string | any | (new (props: any) => React.Component<any, any, any>)>) | (new (props: any) => React.Component<any, any, any>)>;
    createStyleForDiv(): any;
    componentDidMount(): void;
    waitForInstance(reactComponent: ReactComponent, resolve: (value: any) => void, runningTime?: number): void;
    mountReactPortal(portal: ReactPortal, reactComponent: ReactComponent, resolve: (value: any) => void): void;
    batchUpdate(callback?: any): any;
    destroyPortal(portal: ReactPortal): void;
    private getStrategyTypeForProp;
    shouldComponentUpdate(nextProps: any): boolean;
    componentDidUpdate(prevProps: any): void;
    processPropsChanges(prevProps: any, nextProps: any): void;
    private extractDeclarativeColDefChanges;
    private extractGridPropertyChanges;
    componentWillUnmount(): void;
    isDisableStaticMarkup(): boolean;
}

I can't exactly say why ag-grid did this.我不能确切地说为什么ag-grid I found this looking at the typing files.我在查看打字文件时发现了这一点。 I may be incorrect too.我也可能不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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