Skip to main content

Questions tagged [typescript3.0]

Tag for questions specific to new features in TypeScript 3.x. For general TypeScript questions, the correct tag is TypeScript.

typescript3.0
Filter by
Sorted by
Tagged with
803 votes
11 answers
297k views

'unknown' vs. 'any'

TypeScript 3.0 introduces unknown type, according to their wiki: unknown is now a reserved type name, as it is now a built-in type. Depending on your intended use of unknown, you may want to ...
Jac Mos's user avatar
  • 8,646
115 votes
4 answers
79k views

How to define string literal union type from constants in Typescript

I know I can define string union types to restrict variables to one of the possible string values: type MyType = 'first' | 'second' let myVar:MyType = 'first' I need to construct a type like that ...
Can Poyrazoğlu's user avatar
61 votes
3 answers
57k views

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 ...
Leela Venkatesh K's user avatar
57 votes
7 answers
15k views

TypeScript const assertions: how to use Array.prototype.includes?

I am trying to use an array of elements as union type, something that became easy with const assertions in TS 3.4, so I can do this: const CAPITAL_LETTERS = ['A', 'B', 'C', ..., 'Z'] as const; type ...
dols3m's user avatar
  • 1,876
41 votes
1 answer
12k views

How do I get the return type of a class method in TypeScript

In newer TypeScript versions (I think 2.8 onwards?), I can easily obtain the return type of a function: function f() { return "hi"; } type MyType = ReturnType<typeof f>; //MyType is string But ...
flq's user avatar
  • 22.6k
37 votes
3 answers
108k views

Typescript 3 Angular 7 StopPropagation and PreventDefault not working

I have a text input inside a div. Clicking on the input should set it to focus and stop the bubbling of the div click event. I've tried the stopPropagation and preventDefault on the text input event ...
user1019042's user avatar
  • 2,168
37 votes
2 answers
41k views

Do I ever need explicit allowSyntheticDefaultImports if esModuleInterop is true configuring TypeScript transpilation?

I need confirmation on the following theory. According to TS docs, there are two options that can be set in tsconfig.json. --allowSyntheticDefaultImports: Allow default imports from modules with no ...
Konrad Viltersten's user avatar
33 votes
6 answers
35k views

cannot find name 'require' in angular 7(typescript 3.1.3)

My question is why this error shown? ERROR in src/app/p2p/p2p.component.ts(14,16): error TS2580: Cannot find name 'require'. Do you need to install type definitions for node? Try npm i @types/...
ikhwan rozali's user avatar
31 votes
2 answers
20k views

How to 'map' a Tuple to another Tuple type in Typescript 3.0

I have tuple of Maybe types: class Maybe<T>{ } type MaybeTuple = [Maybe<string>, Maybe<number>, Maybe<boolean>]; and I want to turn this into a tuple of types: type ...
Roaders's user avatar
  • 4,445
31 votes
3 answers
45k views

How to clean typescript cache?

I don't know where to look at, and typescript answer here said that it is not cached But it clearly cached the files. because this is my import And yet, it cannot import the file from old path. I ...
Magician's user avatar
  • 1,997
29 votes
3 answers
120k views

Error: Type 'void' is not assignable to type 'ReactNode'

I'm getting an error while calling the function, this is just for practice so I have kept everything inside App.tsx. My class looks like this: enum Actor { None = '', } const initializeCard = () =&...
user1547554's user avatar
25 votes
3 answers
34k views

TypeScript 3.x: Access properties of type unknown

I read from the TypeScript documentation, that you can't access properties from the type unknown: // No property accesses, element accesses, or function calls function f11(x: unknown) { x.foo; /...
josias's user avatar
  • 1,546
24 votes
2 answers
9k views

Preventing inappropriate imports and enforcing project hierarchy in Typescript

In a TS project I'd like the following to be blocked: A file from folder common importing from folder projectA A file from folder projectB importing from folder projectA I'd like the following to be ...
Ben Carp's user avatar
  • 25.8k
18 votes
5 answers
12k views

Generic curry function with TypeScript 3

TypeScript 3.0 introduced generic rest parameters. Up until this point, curry functions had to be annotated in TypeScript with a finite number of function overloads and a series of conditional ...
wagerfield's user avatar
14 votes
3 answers
9k views

TypeScript: generically infer union type member based on a string literal property

TypeScript (v3.2.2) allows me to define a union of interfaces, each with a unique string literal property which can be used as a type guard, e.g. type Device = Laptop | Desktop | Phone; interface ...
Spencer's user avatar
  • 1,271
14 votes
3 answers
14k views

Typescript: Use `enum` values as `type`

I would like to use following enum's values: export enum GenFormats { SHORT_LOWER = 'm/f', SHORT_UPPER = 'M/F', FULL = 'Male/Female' }; as type given below: export interface IGenderOptions { ...
Vikram's user avatar
  • 622
11 votes
1 answer
15k views

How to use yield in Typescript classes

I am fresher to Typescript, while learning from the site, I got to know that yield can be used for Asynchronous Iteration using for-await-of. The below is the function in Javascript. Please help me ...
Deba's user avatar
  • 969
11 votes
3 answers
1k views

Is it possible to Exclude an empty object from a union?

I have a union of two types, one of which is an empty obj. type U = {} | { a: number } // | { b: string } | { c: boolean } .... I would like to exclude the empty object from the union however ...
lonewarrior556's user avatar
10 votes
1 answer
5k views

"Cannot find module" error when using TypeScript 3 Project References

I'm trying to get TypeScript 3's project references working but struggling to import a function from a referenced project. I have a ProjectA that references Shared. Here's the file structure: ...
Carl Rippon's user avatar
  • 4,623
10 votes
1 answer
4k views

Sharing code between TypeScript projects (with React)?

This question: "Cannot find module" error when using TypeScript 3 Project References was somewhat helpful. So was this one: Project references in TypeScript 3 with separate `outDir` But I ...
Dakota's user avatar
  • 183
9 votes
2 answers
15k views

How to represent nested array with typescript

Say I have an array of strings like: const a = ['foo', ['aa'], [['zzz',['bar']]]]; export const acceptsArray = (v: Array<any>) : string => { returns flattenDeep(v).join(' '); }; ...
Alexander Mills's user avatar
9 votes
2 answers
7k views

TS2538: Type 'unique symbol' cannot be used as an index type

I have this: const symbols = { typeMap: Symbol('type.map') } interface LangMap { [key: string]: string | true, golang: string, typescript: string, java: string, swift: string } export ...
Alexander Mills's user avatar
8 votes
4 answers
3k views

Migrate Node.js project to TypeScript from plain ES6

Is started migrating a Node.js project from plain ES6 to TypeScript. What I did: npm install -g typescript npm install @types/node --save-dev Setup tsconfig.json: { "compilerOptions": { ...
Alexander Zeitler's user avatar
8 votes
1 answer
4k views

TypeScript path mapping "Cannot find module a-mapped/a"

I have a project with the following structure: ┌ tsconfig.json │ { │ "compilerOptions": { │ "baseUrl": ".", │ "paths": { "a-mapped/*": ["a/*"] } │ } │ } │ ├ a │ └─ a.ts │ export const ...
rid's user avatar
  • 62.7k
8 votes
1 answer
775 views

Force object to have at least one key ( Object.keys(o).length > 0 )

Say I have this object export interface Name { [key: string]: boolean } const v = <Name>{}; how do I prevent this from compiling? What I want to do is force v to have at least one property:...
user avatar
7 votes
1 answer
8k views

typescript remove optional property

I'm trying to build a dynamic type for a builder export type Builder<T, K extends keyof T> = { [P in K]: (value: T[P]) => Builder<T, K>; } & { build(): Readonly<T>; };...
Francis's user avatar
  • 3,365
7 votes
1 answer
10k views

Why can't I use StyleProp<ViewStyle> on my own component

I'm using TypeScript/Vscode for coding my React Native app. I want code completion on my custom components for style just like React Native's own View component does. style prop View is defined like ...
Can Poyrazoğlu's user avatar
7 votes
2 answers
18k views

Type 'T' is not assignable to type 'string'

Here is the problem : I wanted to make a function that gets a parameter and returns the same type. I made the simpliest example possible : type Test = <T>(arg: T) => T; const test: Test = (...
ImFonky's user avatar
  • 825
7 votes
1 answer
2k views

In TypeScript, is there a way to restrict extra/excess properties for a Partial<T> type when the type is a parameter to a function?

Is there a standard way to get Scenario 1 to have a compile error for not specifying known properties, just like in Scenario 2? Or is there some workaround? class Class2 { g: number; } class ...
CShark's user avatar
  • 1,612
7 votes
1 answer
1k views

Compile single .ts file to stdout

I am looking to compile a single .ts file to stdout, something like this: tsc foo.ts > foo.js is this possible somehow? I want to control where the output goes without using a tsconfig.json file.
Alexander Mills's user avatar
6 votes
1 answer
12k views

Generics for Arrays in TypeScript 3.0

So I see that 3.0 comes with generic typings for rest parameters so you can do something like this: static identity<T extends any[]>(...values: T): T; Is it possible to get something similar ...
wjohnsto's user avatar
  • 4,413
6 votes
3 answers
10k views

Typescript callback arguments, which are resolved using a function

I have a situation, where I call a function with some arguments, which are resolved using another function. This is how the code looks. function getArgs (): [string, number] { return ['hello world'...
Aman Virk's user avatar
  • 3,937
6 votes
2 answers
13k views

TypeScript infer return type from used arguments

I'm not sure if this has been asked under different terminology. I've got a function that takes two arguments, where both can (independently) be a number, string, or undefined. The returned value is ...
Can Poyrazoğlu's user avatar
6 votes
2 answers
2k views

Performance issue with @angular/material autocomplete

I am using Angular/Material Autocomplete. While loading data to the Autocomplete getting serious performance issues,like the rendering takes around 30 seconds and it takes more than 10 seconds to ...
Vignesh's user avatar
  • 2,386
5 votes
2 answers
3k views

difference between `unknown` and `void`

According to this answer: Using void instead means that forEach promises not to use the return value, so it can be called with a callback that returns any value According to the TypeScript 3.0 ...
William Stanley's user avatar
5 votes
1 answer
12k views

Using generic type with class - type T does not satisfy the constraint

export type OptionsToType<T extends Array<{ name: Array<string>, type: keyof TypeMapping }>> = { [K in T[number]['name'][0]]: TypeMapping[Extract<T[number], { name: K }>['...
Alexander Mills's user avatar
5 votes
1 answer
5k views

Extending built-in types in Typescript

I have the following structure: project |- types |- global.d.ts |- string.d.ts |- wdio.d.ts |- src |- Models |- Resources |- Components |- Extensions |- string.ts ...
Zoltán Fekete's user avatar
5 votes
1 answer
9k views

TS1238: Unable to resolve signature of class decorator when called as an expression

I am seeing the following compilation error: TS1238: Unable to resolve signature of class decorator when called as an expression. Here is the code: const fdec = function(target:any, field: any, ...
Alexander Mills's user avatar
5 votes
1 answer
6k views

How to fix Type not assignable to LibraryManagedAttributes for react router render prop

I recently updated all react libraries to the latest and all of its typings. I am facing an issue with typescript compilation. It is saying [ts] Type '{ history: History<any>; location: ...
pavinan's user avatar
  • 1,323
5 votes
0 answers
596 views

How can I use multiple wildcards in Typescript module declarations?

In my ReactJS project I use the svg-transform-loader package in WebPack which updates some colors of my SVGs on the fly. import starOutlineIcon from 'images/star-outline.svg' import ...
Pascal Corpet's user avatar
4 votes
2 answers
537 views

Conditional types with TypeScript

Say I have this: type TypeMapping = { Boolean: boolean, String: string, Number: number, ArrayOfString: Array<string>, ArrayOfBoolean: Array<boolean> } export interface ElemType ...
Alexander Mills's user avatar
4 votes
2 answers
902 views

Declare arbitrarily nested array (recursive type definition)

Say I have a function like: const nested = function(v: string | Array<string> | Array<Array<string>>){...} the problem is v might be nested 5 or 6 levels deep. How can I declare a ...
user avatar
4 votes
2 answers
4k views

tsc creates src folder in dist

I have this folder structure: - src - subdir1 - subdir2 This is my tsconfig.json: { "compilerOptions": { "target": "es2015", "module": "commonjs", "sourceMap": true, "...
Alexander Zeitler's user avatar
4 votes
1 answer
6k views

Argument of type '(event: BlahEvent) => void' is not assignable to parameter of type 'EventListenerOrEventListenerObject'

I've read and tried suggestions found in Argument of type '(e: CustomEvent) => void' is not assignable to parameter of type 'EventListenerOrEventListenerObject' but haven't been ...
Caius Jard's user avatar
  • 73.8k
4 votes
0 answers
289 views

How to use interfaces from a .d.ts file outside the current project through project references in TypeScript?

I am new to TypeScript. I am working on a React frontend and Express backend project. My project folder structure is like the following: server/ ├─ src/ │ ├─ index.d.ts │ ├─ someFile.ts ├─ tsconfig....
user12055579's user avatar
4 votes
0 answers
7k views

Argument of type 'any' is not assignable to parameter of type 'never'

got this error below, error TS2345: Argument of type 'any' is not assignable to parameter of type 'never'. HandEvaluator.prototype.evaluateStraight = function (hand) { var consecutives = []; ...
Chris C's user avatar
  • 91
3 votes
2 answers
3k views

What's the difference between TypeScript const assertions and declarations?

I just read about the new const assertion feature in TypeScript 3.4 RC and I'm not seeing how it does anything differently from using const declarations. I tested this out using an example from the ...
Adam's user avatar
  • 3,916
3 votes
1 answer
2k views

Recursively exclude readonly properties in Typescript

I'm pretty sure its my lack of experience utilizing complex generics so I'm hoping someone has ideas on how to accomplish this. My use case is for creating "form types" for my React/Formik form values ...
ericchernuka's user avatar
3 votes
2 answers
10k views

Properly extend stream.Transform class with TypeScript

I have this code: import * as stream from 'stream'; export class JSONParser extends stream.Transform { lastLineData = ''; objectMode = true; constructor() { super(); } ...
Alexander Mills's user avatar
3 votes
2 answers
8k views

TypeScript 3: property is missing in type

I'm working on a ReactJS app with TypeScript. I was using TypeScript 2.8 with no problem, but 2.9 and 3 give me a new error. import * as React from 'react'; class ExampleComponent extends React....
BenjiFB's user avatar
  • 4,611