我有一个npm包,它具有以下类型定义(简化):
./node_modules/ag-grid-react/main.d.ts
export declare class AgGridReact extends Component<AgGridReactProps, {}> {}./node_modules/ag-grid-react/lib/agGridReact.d.ts
export declare class AgGridReact extends Component<AgGridReactProps, {}> {
gridOptions: GridOptions;
api: GridApi | null;
columnApi: ColumnApi;
}我正在使用反应性组件中的组件,如下所示:
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}/>)
}问题:
打字本确实抱怨没有columnApi。很遗憾,它从main.d.ts中选择了错误的类型
我发现可以直接从agGridReact.d.ts导入类型,并按如下方式使用:
import {AgGridReact as AgGridReactType} from 'ag-grid-react/lib/agGridReact'
...
const grid = useRef<AgGridReactType>(null)问题:
这是处理这个问题的正确方法吗?类型记录是否足够聪明,不导入可能导致我的包大小增加的./node_modules/ag-grid-react/lib/agGridReact.ts文件?
我已经搜索了很多,但是没有找到任何关于导入类型的东西,只从node_modules子文件夹。
发布于 2020-05-21 23:18:34
我会尽量回答这个问题:
让我们假设有一个xyz库,并且它有以下文件:
xyz/lib/main.ts:
export const test = 1000和
xyz/main.ts:
export * from './lib/main.ts'
export const test = 'foo bar'我想在我的app.ts中使用,而且我只知道它的main.ts文件,因为我认为它是从库导出所有东西的主文件。所以我最有可能做的是:
app.ts:
import { test } from './xyz/main'
console.debug(test) // it will print 'foo bar'现在,有人在图书馆里评论这一行:
xyz/main.ts:
export * from './lib/main.ts'
// export const test = 'foo bar'现在,我的app.ts将打印什么?它将打印1000。
同样的事情也发生在ag-grid-react身上。It (ag-grid-react/main.d.ts)是覆盖--显然正确(更好)的类声明出现在ag-grid-react/lib/agGridReact.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:
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;
}我不能确切地说出为什么ag-grid会这么做。我在看打字文件时发现了这个。我可能也错了。
https://stackoverflow.com/questions/61944463
复制相似问题