Below you find a list of common TypeScript errors along with the buggy code and the code which fixed the issue.
TS1055
error TS1055: Type ‘AxiosPromise‘ is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
type PreKeyAuth = 'Invalid' | 'Unknown' | 'Valid';
TS1192
error TS1192: Module ‘.../logdown‘ has no default export.
Buggy Code
1 2 3 4 5
declareclassLogdown{ // ... }
export = Logdown;
Fixed Code
1 2 3 4 5
declareclassLogdown{ // ... }
exportdefault Logdown;
TS1202
error TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using ‘import * as ns from “mod”‘, ‘import {a} from “mod”‘, ‘import d from “mod”‘, or another module format instead.
Buggy Code
1
import sinon = require('sinon');
Fixed Code
1
import * as sinon from'sinon';
TS1218
error TS1218: Export assignment is not supported when ‘–module’ flag is ‘system’.
Buggy Code
1 2 3 4 5
classLRUCache{ // ... }
export = LRUCache;
Fixed Code
1 2 3
exportclassLRUCache{ // ... }
TS2488
Type ‘{ [label: string]: string; }’ must have a ‘Symbol.iterator‘ method that returns an iterator.
Solution
You have to add a property called [Symbol.iterator] to your object. The value of this property has to return an iterator. Here you can learn how to create an iterator.
error TS2345: Argument of type ‘x‘ is not assignable to parameter of type ‘y‘.
1 2 3 4 5 6
error TS2345: Argument of type '(records: T[]) => void' is not assignable to parameter of type '(value: [{}, {}, {}, {}, > {}, {}, {}, {}, {}, {}]) => void | PromiseLike<void>'. Types of parameters 'records' and 'value' are incompatible. Type '[{}, {}, {}, {}, {}, {}, {}, {}, {}, {}]' is not assignable to type 'T[]'. Types of property 'pop' are incompatible. Type '() => {}' is not assignable to type '() => T'. Type '{}' is not assignable to type 'T'.
error TS2564: Property ‘key’ has no initializer and is not definitely assigned in the constructor.
Info: This error occurs with TypeScript 2.7 in “strict” mode because TypeScript 2.7 introduced a new flag called --strictPropertyInitialization, which tells the compiler to check that each instance property of a class gets initialized in the constructor body, or by a property initializer. See Strict Class Initialization.
Remove /// <reference path="..." /> in .d.ts files
TS2656
error TS2656: Exported external package typings file ‘../proteus.d.ts‘ is not a module. Please contact the package author to update the package definition.
Buggy Code
1 2
declaremodule Proteus { }
Fixed Code
1 2
exportmodule Proteus { }
TS2668
error TS2668: ‘export’ modifier cannot be applied to ambient modules and module augmentations since they are always visible.
Info
Ambient modules
To describe the shape of libraries not written in TypeScript, we need to declare the API that the library exposes. We call declarations that don’t define an implementation “ambient”. Typically, these are defined in .d.ts files. If you’re familiar with C/C++, you can think of these as .h files.
With module augmentation, users have the ability to extend existing modules such that consumers can specify if they want to import the whole module or just a subset.
error TS5055: Cannot write file ‘/Users/bennyn/projects/wireapp/wire-webapp-lru-cache/dist/commonjs/LRUCache.d.ts‘ because it would overwrite input file.
error TS6059: File ‘/root/project/packages/server/package.json‘ is not under ‘rootDir’ ‘/root/project/packages/server/src‘. ‘rootDir’ is expected to contain all source files.
Buggy Code
1
import pkg from'../../../../package.json';
Fixed Code
1
const pkg = require('../../../../package.json');
TS6133
error TS6133: ‘volume‘ is declared but its value is never read.
public create<T>(tableName: string, primaryKey: string, entity: T): Promise<string> { if (entity) { returnthis.db[tableName].add(entity, primaryKey).catch((error: Dexie.DexieError) => { if (error instanceof Dexie.ConstraintError) { const message: string = `Record "${primaryKey}" already exists in "${tableName}". You need to delete the record first if you want to overwrite it.`; thrownew RecordAlreadyExistsError(message); } else { throw error; } }); } const message: string = `Record "${primaryKey}" cannot be saved in "${tableName}" because it's "undefined" or "null".`; returnPromise.reject(new RecordTypeError(message)); } }
public create<T>(tableName: string, primaryKey: string, entity: T): Promise<string> { if (entity) { returnthis.db[tableName].add(entity, primaryKey).catch((error: Dexie.DexieError) => { if (error instanceof Dexie.ConstraintError) { const message: string = `Record "${primaryKey}" already exists in "${tableName}". You need to delete the record first if you want to overwrite it.`; thrownew RecordAlreadyExistsError(message); } else { throw error; } }); } const message: string = `Record "${primaryKey}" cannot be saved in "${tableName}" because it's "undefined" or "null".`; returnPromise.reject(new RecordTypeError(message)); } }