angular - 如何从通常未全局加载的包中全局加载类型?
问题描述
我更喜欢import
仅用于在文件中包含实际功能,并使用 mytsconfig
包含所有必要的类型。
这是我的应用程序中的一个示例减速器:
import { createReducer } from '@ngrx/store';
const initialState: State = ['a', 'b', 'c', 'd'];
const reducer = createReducer(
initialState,
);
export function appReducer(state: State, action: Action): State {
return reducer(state, action);
}
我已声明State
in custom_types/typings.d.ts
,并已包含custom_types
在tsconfig.compileOptions.typeRoots
. 这工作正常。
Action
未定义。我可以将其包含Action
在import
语句中,但由于它仅用于键入而不用于功能,因此我更愿意通过我的tsconfig
.
我尝试以@ngrx
多种方式在全球范围内包含类型:
- 添加
@ngrx/store
到typeRoots
- 添加
@ngrx/store/index.d.ts
到typeRoots
- 添加
import '@ngrx/store'
到custom_types/typings.d.ts
但是编译器继续说这Action
是未定义的。
如何@ngrx
在我的项目中全局包含所有类型?
解决方案
在custom_types/global-types.d.ts
:
type Action = import("@ngrx/store").Action
或者
import * as store from "@ngrx/store"
declare global {
type Action = store.Action
}
推荐阅读
- python - AWS python lambda 数据狗集成
- nginx - nginx 镜像模块对大型响应主体的“对等连接重置”
- python - 查找具有给定数字和列表的边界
- vue.js - 如何在 vue js 中使用路由器链接?
- r - 在 t + 1 秒时搜索时间序列值
- recursion - Labview中的递归 - 是什么导致它挂起?
- windows - openstack如何使用管理员密码创建Windows服务器实例
- python - python zip文件命中用户警告:名称重复
- flutter - 在构建期间调用 setState() 或 markNeedsBuild()。Flutter 提供程序包
- javascript - 我无法使用 javascript 和 Puppeteer 获取标签中包含的数值