How do you explicitly set a new property on `window` in TypeScript?

I setup global namespaces for my objects by explicitly setting a property on window. window.MyNamespace = window.MyNamespace || {}; TypeScript underlines MyNamespace and complains that: The...

Is there a way to "extract" the type of TypeScript interface property?

Let's suppose there's a typing file for library X which includes some interfaces. interface I1 { x: any; } interface I2 { y: { a: I1, b: I1, c: I1 } z: any } In order to work with...

How do you format code on save in VS Code

I would like to automatically format TypeScript code using the build-in formatter when I save a file in Visual Studio Code. I'm aware of the following options, but none of them is good...

Class constructor type in typescript?

How can I declare a class type, so that I ensure the object is a constructor of a general class? In the following example, I want to know which type should I give to AnimalClass so that it could...

Accessing different properties in a typescript union type

I'm creating a function that handles objects from the database. I have two different data structures where the same property has a different name. I can't change that, so I have to handle it in...

Typescript Promise rejection type

How do I set the type of the rejection of my promise? Let's say I do: const start = (): Promise<string> => { return new Promise((resolve, reject) => { if (someCondition) { ...

Modules not found when using express

I want to expose REST service in electron app and I want to use expressJS with electron, as tutorial states I added express and @types/express. I tried to expose a "get " but when I build and run...

vscode typescript: 'Add all missing imports' shortcut

I am working on a typescript project (typescript3.x). I recently noticed the Add all missing imports when I click on the bulb which comes when I am using more than one types which are not yet...

Is it possible to precisely type _.invert in TypeScript?

In lodash, the _.invert function inverts an object's keys and values: var object = { 'a': 'x', 'b': 'y', 'c': 'z' }; _.invert(object); // => { 'x': 'a', 'y': 'b', 'z': 'c' } The lodash typings...

How to load json via webpack file-loader?

I need to load several json files via file-loader and then in runtime load and parse them via another library, but webpack transpiles json file into js adding 'module.exports =... at beginning of...

Ng serve throwing @angular/core/core has no exported member 'eeFactoryDef'

Im getting like 50 errors that all say that angular/core/core has no exported member 'eeFactoryDef'. They come from different node_modules, for example angular/cdk, ng-bootstrap, ngx-pipes,...

How to make Typescript throw runtime error?

import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { ...

Typescript: Enforce a type to be "string literal" and not <string>

Problem Is there a way in Typescript to define a type that is only a string literal, excluding string itself? Note that I am not talking about a certain list of string literal; for which, a simple...

How can I supply types to Vuex mapState functions?

I'm using Typescript in my Vuex component and would like to provide types to the mapping functions in mapState. Intuitively, I wrote it like this: @Component({ computed: { ...mapState(...

Typescript: generic functional type/interface for arrow function

Ok, I have the following scenario: type IHashFn = (arg: string) => number; const hash: IHashFn = (arg) => { return 42; } So far, so good. Now I want the function to be generic. const hash: <T>...

ReactJS and Typescript : refers to a value, but is being used as a type here (TS2749)

I'm coding a ReactJS class with Typescript and Material-ui, in a .tsx file. In one of my custom components, I want to create a reference to one of the components that I use in my custom...

how to type define a zip function in typescript

Say I want to create a zip function: function zip(arrays){ // assume more than 1 array is given and all arrays // share the same length const len = arrays[0].length; const...

Typescript "Generics" Syntax + Angle Brackets

I'm having a little bit of trouble understanding the purpose behind angle brackets in Typescript. Often, they're used for generics. This is clearest to me for functions. For example, if you've got...

Unknown Option error from Babel in React-Native app

I am building a react-native app with typescript in order to learn react native. Once I run the app with expo start and try to run on emulator I get this error: index.js: [BABEL]...

React, Typescript - Cannot find module ... or its corresponding type declarations

I created a new project using create-react-app and yarn 2 in vs code. The editor throws errors while importing every installed library like this: Cannot find module 'react' or its corresponding...

Props typing in Vue.js 3 with TypeScript

I'm trying to type hint my props in a Vue 3 component, with composition API. So, I'm doing this: <script lang="ts"> import FlashInterface from '@/interfaces/FlashInterface'; import { ref } from...

TypeScript is not picking up my custom type definitions in tsconfig

I'm using tsconfig to check my js files. I have my custom type definitions in the custom_types\custom.d.ts tsconfig.json { "compilerOptions": { "target": "es2020", "module": "commonjs", ...

To load an ES module, set "type": "module" in the package.json or use the .mjs extension

I am trying to run on desktop this repository a vscode extension. I cloned it locally and run npm install Press f5 on vscode editor and got an error Process exited with code 1 (node:1404) Warning:...

Shaders with Typescript and React three fiber

I'm trying to use shaders with React-three-fiber and Typescript. Shader file: import { ShaderMaterial } from "three" import { extend } from "react-three-fiber" class CustomMaterial extends...

How to allow a function return type undefined

I think that typescript has many unobvious places, making it not strict and not correct. I want use undefined as functions return type. Because in reality it is undefined, not void or some other...

Lerna, yarn, and Typescript: Cannot find module or its corresponding type declarations

I have a lerna + yarn workspaces monorepo, with independent versioning. I'm having trouble importing a package in its sibling. I have two packages, one of which should depend on the other, as...

How can I add style prop as control in Storybook?

I am building a design system library in React and TypeScript using Storybook. Most components support setting custom styles using the style prop. I am trying to reflect this in Storybook using...

Streaming response body to file in typescript: Property 'pipe' does not exist on type 'ReadableStream<Uint8Array>'

I am able to fetch a binary body from an API to write it to a file in node. const fileStream = fs.createWriteStream(filePath); fetch(apiURL).then((downloadResponse) => { ...

Angular v13 Jest with nx test - SyntaxError: Cannot use import statement outside a module at Runtime.createScriptFromCode

I tried to follow every comment with a possible solution here to the letter. I relied on an example project on github as well which works perfectly. This also started to happen to me after...

TypeError: MiniCssExtractPlugin is not a constructor

[webpack-cli] TypeError: MiniCssExtractPlugin is not a constructor at module.exports (/home/andrey/smartadmin-app/webpack.config.js:70:9) at loadConfigByPath...