首页 > 解决方案 > 如何从通常未全局加载的包中全局加载类型?

问题描述

我更喜欢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);
}

我已声明Statein custom_types/typings.d.ts,并已包含custom_typestsconfig.compileOptions.typeRoots. 这工作正常。

Action未定义。我可以将其包含Actionimport语句中,但由于它仅用于键入而不用于功能,因此我更愿意通过我的tsconfig.

我尝试以@ngrx多种方式在全球范围内包含类型:

但是编译器继续说这Action是未定义的。

如何@ngrx在我的项目中全局包含所有类型?

标签: angulartypescriptngrx

解决方案


custom_types/global-types.d.ts

type Action = import("@ngrx/store").Action

或者

import * as store from "@ngrx/store"

declare global {
  type Action = store.Action
}

推荐阅读