What is the best way to import types from a package that is not usually imported globally?

I like to use the import statement only for importing actual functionality in a file, and I rely on my tsconfig to include all necessary types.

Here is an example of a reducer in my application:

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);
}

I have defined State in custom_types/typings.d.ts, and have included custom_types in

tsconfig.compileOptions.typeRoots
. This setup works well for me.

The Action is currently undefined. Even though I could add Action in the import statement, since it is used solely for typing purposes, I would rather include it through my tsconfig.

I have attempted various ways to globally include @ngrx typings:

  • Adding @ngrx/store to typeRoots
  • Adding @ngrx/store/index.d.ts to typeRoots
  • Adding import '@ngrx/store' to custom_types/typings.d.ts

However, the compiler keeps indicating that Action is undefined.

What is the best way to globally include all @ngrx typings in my project?

Answer №1

Within the file custom_types/global-types.d.ts:

The definition of Action is imported from "@ngrx/store"

Alternatively, you can import everything as store from "@ngrx/store" and declare a global type:


import * as store from "@ngrx/store"

declare global {
  type Action = store.Action
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Filtering an Array in Angular 4: A Simplified Guide

I am currently working on a data table project using data from Firebase. The issue I am facing is related to route navigation with a 'name' parameter. When I click on a link, I want to display only the data that matches the passed 'name&apos ...

How can you cycle through multi-dimensional arrays in Angular 2?

UPDATE#2 Unfortunately, I've been unable to successfully implement either of the suggestions provided. It's getting late, so I'm going to call it a night and revisit this tomorrow. A special thanks to Pradeep Jain for offering assistance. ...

The custom validation feature in Angular 4 is failing to function as expected

Currently, my focus is on Angular 4 where I have developed a custom validator for checking CGPA values (to ensure it is between 2.0 and 4.0). Although the predefined `Validators.required` works fine, my custom validator seems to be not triggering as expect ...

Tips for obtaining the OneSignal playerID

When launching the app, I need to store the playerID once the user accepts notifications. This functionality is located within the initializeApp function in the app.component.ts file. While I am able to retrieve the playerID (verified through console.log) ...

Getting the URL segment while using a CanActivate guard

Ensuring users authenticate before accessing a requested page using a guard is crucial. In one specific scenario, a user may enter a URL with query parameters like To retrieve both the parameters and the URL segment within the guard, a piece of code has ...

Component in Angular 2 for blocking UI

How can you effectively block the user interface of an Angular 2 component? Is this the correct method to use? <component [blockUI]="true"></component> ...

Encountered an issue resolving peer dependency during package installation

I recently developed an Angular 9 library that consists of several packages. However, when running npm install, I encountered an error with one of the external packages stating 'Could not resolve peer dependency @angular/common@"^8.2.6"". I ...

What is the method for displaying xAxis categories for each individual chart in the Bar-Highcharts library?

Is there a way to display categories at the top of each bar chart, similar to the image shown? To achieve this, I have configured the bar chart options as follows, omitting any xAxis category labels: options: { title: null, chart: ...

Unit testing in Angular involves using the spyOn function to mock classes or functions imported

Trying to ensure a function call from node_modules is made using a spy. For instance, in obs.d.ts: export declare module Obs { class MyService { func(params: Object): Promise<void>; Thus, I aim to spy on func() In my ...

Trouble arises when selecting shapes in Angular 6 with FabricJS

Currently, I am experimenting with fabricjs and drawing different shapes. Interestingly, when I draw the shapes, they appear accurately on the canvas. However, when I try to switch to selection mode, I encounter an issue where I am unable to select the ind ...

"Implement a feature to populate dropdown options in an Angular select component with retrieved

I'm currently utilizing ngx-select-dropdown (https://github.com/manishjanky/ngx-select-dropdown) and my goal is to fetch a list of Cities in an array of objects to display them in a select menu dropdown autocomplete. However, I'm encountering dif ...

What is the best way to refine object T types by supplying an array of exclusions (keyof T)[]?

What's the best way to create a type guard that can exclude keys from an object in TypeScript? Below is my getExcludedKeys function, which aims to filter out certain keys from an object. However, I'm encountering an issue where the type guard is ...

Are you looking to use the 'any' type instead of the 'Object' type? Angular Services can help you with that

I encountered the following error message: Argument of type 'OperatorFunction<APISearch[], APISearch[]>' is not assignable to >parameter of type 'OperatorFunction<Object, APISearch[]>'. The 'Object' type is ...

Encountering issues during the installation of the Angular/CLI software

Whenever I execute the command below: npm install -g @angular/cli I encounter this error message: C:\Users\AA>npm install -g @angular/cli npm WARN deprecated <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="80f ...

The concept of a singleton design pattern is like a hidden treasure waiting to be

My approach to implementing the singleton pattern in a typescript ( version 2.1.6 ) class is as follows: export class NotificationsViewModel { private _myService: NotificationService; private _myArray: []; private static _instance: Notificatio ...

The CSS scale property is not working as expected when used in a React.js application, specifically

working environment ・next.js ・react ・typescript https://www.youtube.com/watch?v=ujlpzTyJp-M A Toolchip was developed based on the referenced video. However, the --scale: 1; property is not being applied. import React, { FunctionComponent ...

The concept of recursive generics in combination with array inference

I'm struggling to develop a couple of generic recursive types to adjust the structure of existing types. I can't figure out why the sections detecting arrays and nested objects are not being activated. Any thoughts on what might be going wrong? ...

Why is Mongoose returning null when using findOne?

Here is a sample request: interface IGetFullnameRequest extends IAuthenticatedRequest { readonly body: Readonly<{ fullname: string; }>; } This is the controller function to get the fullname: const getFullname = async (req: IGetFullna ...

Does bringing in an object and utilizing it within an Array result in the initial item becoming undefined?

I am currently working on importing a few files to create an object: // otis.ts export const otisHeadline = 'Realizing the office of the future for UTC'; export const otisPreview = toCloudinaryUrl('otisPreview1.png'); export const otis ...

Using Angular with Cordova - How to troubleshoot using TypeScript files?

Currently in the process of debugging the application using source files written in Typescript. Despite attempting to debug with Chrome, I have been limited to only being able to debug the Javascript files generated by Cordova which has proven difficult. ...