TypeScript Error Codes
Crack the code to your compiler problems with this ultimate list of common TypeScript errors. Each entry includes the faulty code and its fixed version, ensuring you can quickly resolve any issue and get back to coding.
Boost your productivity even further by installing the Pretty TypeScript Errors - VS Code Extension to see the error translations directly in your IDE.
All errors are categorized by the TypeScript team into the following ranges:
- 1xxx for syntactic errors
- 2xxx for semantic errors
- 4xxx for declaration emit errors
- 5xxx for compiler options errors
- 6xxx for command line errors
- 7xxx for type errors
The Ultimate Fix-it List:
- TS1002
- TS1005
- TS1006
- TS1015
- TS1016
- TS1029
- TS1035
- TS1036
- TS1038
- TS1039
- TS1046
- TS1054
- TS1055
- TS1056
- TS1064
- TS1066
- TS1068
- TS1070
- TS1095
- TS1099
- TS1103
- TS1107
- TS1109
- TS1117
- TS1121
- TS1127
- TS1128
- TS1149
- TS1155
- TS1160
- TS6133
- TS1163
- TS1175
- TS1183
- TS1192
- TS1196
- TS1202
- TS1208
- TS1218
- TS1219
- TS1225
- TS1228
- TS1232
- TS1240
- TS1241
- TS1243
- TS1244
- TS1247
- TS1248
- TS1254
- TS1259
- TS1274
- TS1280
- TS1308
- TS1309
- TS1323
- TS1337
- TS1341
- TS1345
- TS1355
- TS1357
- TS1361
- TS1363
- TS1368
- TS1371
- TS1375
- TS1378
- TS1385
- TS1389
- TS1431
- TS1432
- TS1434
- TS1450
- TS1470
- TS1471
- TS1479
- TS1484
- TS2300
- TS2304
- TS2305
- TS2306
- TS2307
- TS2314
- TS2315
- TS2318
- TS2322
- TS2335
- TS2339
- TS2341
- TS2344
- TS2345
- TS2348
- TS2349
- TS2350
- TS2351
- TS2352
- TS2353
- TS2355
- TS2361
- TS2362
- TS2364
- TS2365
- TS2366
- TS2367
- TS2368
- TS2369
- TS2370
- TS2371
- TS2372
- TS2377
- TS2378
- TS2383
- TS2389
- TS2390
- TS2391
- TS2393
- TS2394
- TS2395
- TS2397
- TS2403
- TS2411
- TS2415
- TS2416
- TS2420
- TS2425
- TS2428
- TS2430
- TS2440
- TS2445
- TS2448
- TS2451
- TS2454
- TS2456
- TS2459
- TS2475
- TS2476
- TS2488
- TS2491
- TS2497
- TS2498
- TS2503
- TS2504
- TS2507
- TS2511
- TS2512
- TS2515
- TS2528
- TS2531
- TS2532
- TS2533
- TS2536
- TS2537
- TS2538
- TS2539
- TS2540
- TS2550
- TS2551
- TS2552
- TS2554
- TS2556
- TS2558
- TS2559
- TS2561
- TS2564
- TS2567
- TS2571
- TS2574
- TS2577
- TS2580
- TS2582
- TS2583
- TS2584
- TS2588
- TS2589
- TS2595
- TS2604
- TS2611
- TS2612
- TS2613
- TS2614
- TS2616
- TS2617
- TS2632
- TS2636
- TS2637
- TS2638
- TS2652
- TS2654
- TS2656
- TS2661
- TS2663
- TS2664
- TS2665
- TS2668
- TS2669
- TS2674
- TS2677
- TS2678
- TS2680
- TS2683
- TS2684
- TS2686
- TS2687
- TS2688
- TS2689
- TS2691
- TS2693
- TS2694
- TS2695
- TS2705
- TS2706
- TS2707
- TS2709
- TS2713
- TS2715
- TS2717
- TS2720
- TS2722
- TS2724
- TS2730
- TS2732
- TS2739
- TS2740
- TS2741
- TS2742
- TS2749
- TS2769
- TS2774
- TS2779
- TS2786
- TS2790
- TS2792
- TS2794
- TS2802
- TS2813
- TS2814
- TS2820
- TS2821
- TS2823
- TS2834
- TS2835
- TS2845
- TS2855
- TS4010
- TS4020
- TS4023
- TS4025
- TS4031
- TS4055
- TS4060
- TS4063
- TS4075
- TS4081
- TS4104
- TS4111
- TS4112
- TS4113
- TS4114
- TS5023
- TS5024
- TS5025
- TS5042
- TS5054
- TS5055
- TS5058
- TS5069
- TS5070
- TS5083
- TS5087
- TS5093
- TS5095
- TS5097
- TS5101
- TS5110
- TS6053
- TS6059
- TS6133
- TS6138
- TS6192
- TS6196
- TS6198
- TS6234
- TS6385
- TS6387
- TS6504
- TS7005
- TS7006
- TS7008
- TS7009
- TS7010
- TS7016
- TS7017
- TS7022
- TS7023
- TS7026
- TS7027
- TS7029
- TS7030
- TS7031
- TS7034
- TS7038
- TS7041
- TS7044
- TS7053
- TS8020
- TS17000
- TS17004
- TS17009
- TS17019
- TS18003
- TS18004
- TS18016
- TS18026
- TS18028
- TS18046
- TS18047
- TS18048
- TS18049
- TS18052
- TS71002
- TS80001
- TS80005
TS1002
Unterminated string literal.
Broken Code ❌
Fixed Code ✔️
You have to close the string literal with an ending '
:
If you want to support multiline text, then you would have to use string concatenation:
Another solution would be using a template literal:
TS1005
'=' expected.
Broken Code ❌
Fixed Code ✔️
You need to assign your type declaration using the =
character:
Alternatively, you can declare an interface:
';' expected.
Broken Code ❌
Fixed Code ✔️
When using an arrow function expression, you can implicitly return values by wrapping the object literal in parentheses:
TS1006
A file cannot have a reference to itself.
Broken Code ❌
Fixed Code ✔️
You cannot reference a file to itself (causes recursive loop). To fix the problem you have to update the reference path to point to another declaration file:
TS1015
Parameter cannot have question mark and initializer.
Broken Code ❌
Fixed Code ✔️
TS1016
A required parameter cannot follow an optional parameter.
Broken Code ❌
Fixed Code ✔️
The easiest way to fix the error is to make age
optional as well:
Alternatively, you can flip the order of middleName
and age
. Be aware that this breaks the contract (signature) of the function and is considered a "breaking change":
You could also make middleName
non-optional:
Yet another solution would be to assign a default value to middleName
so it won't be optional by default. This allows age
to be optional then:
TS1029
'public' modifier must precede 'abstract' modifier.
Broken Code ❌
Fixed Code ✔️
They keywords public
, private
, and protected
define the access to a class member. Access modifiers have to be defined first in TypeScript.
Solution 1:
Solution 2:
The visibility is public
by default, so you don't have to explicitly declare it:
Video Tutorial
TS1035
Only ambient modules can use quoted names.
Broken Code ❌
Fixed Code ✔️
To turn a module into an ambient module, you have to prefix it with the declare
keyword:
TS1036
Statements are not allowed in ambient contexts.
Broken Code ❌
Fixed Code ✔️
With declare global
an ambient context is created. TypeScript does not allow statements in such ambient context declaration which is why we have to change the statement into a declaration:
If you don't want client
to be a function, you have to use the
var` keyword:
TS1038
A 'declare' modifier cannot be used in an already ambient context.
Broken Code ❌
Fixed Code ✔️
TS1039
Initializers are not allowed in ambient contexts.
Broken Code ❌
Fixed Code ✔️
Within a declaration file (ambient context), you should define types instead of implementing code. Consequently, you'll need to replace the initialization with a type annotation:
TS1046
Top-level declarations in .d.ts files must start with either a 'declare' or 'export' modifier.
Broken Code ❌
Fixed Code ✔️
If you want to export a constant from a definition file (d.ts
), then you have to use the export
modifier:
TS1054
TS1054: A
get
accessor cannot have parameters.
Broken Code ❌
Solution:
To fix this issue, you need to convert the get
accessor to a regular method since TypeScript does not allow parameters for getters. This change allows the method to accept arguments as required.
Fixed Code ✔️
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.
Broken Code ❌
Fixed Code ✔️
TS1056
Accessors are only available when targeting ECMAScript 5 and higher.
Broken Code ❌
Fixed Code ✔️
Set the "target" property in your "tsconfig.json" file to "es5" or higher:
TS1064
The return type of an async function or method must be the global Promise type. Did you mean to write 'Promise '?
Broken Code ❌
Fixed Code ✔️
If your function is asynchronous, your return value must be wrapped with Promise<...>
:
TS1066
In ambient enum declarations member initializer must be constant expression.
Broken Code ❌
Fixed Code ✔️
Try to replace your enum declaration with a type:
TS1068
Unexpected token. A constructor, method, accessor, or property was expected.
Broken Code ❌
Fixed Code ✔️
Functions that are part of a class are being called "method". The method of a class is defined without the function
keyword:
TS1070
'private' modifier cannot appear on a type member.
Broken Code ❌
Fixed Code ✔️
Interfaces are structures that define the public contract. This prohibits you from using private
modifiers. Only public
and protected
can be used. To solve the problem, the private
keyword must be removed from the name
property of the Animal
interface:
Video Tutorial
TS1095
A 'set' accessor cannot have a return type annotation.
Broken Code ❌
Fixed Code ✔️
You have to remove the return type from the "set" accessor:
TS1099
Type argument list cannot be empty.
Broken Code ❌
This error occurs because TypeScript does not allow empty type argument lists (<>
).
Fixed Code ✔️
The method response.json()
does not expect generic parameters to be provided. You don't need to provide any generic type parameters. Simply remove the empty type argument list (<>
) from the method call:
If you want to strongly type the result, you can cast it using as
:
TS1103
'for await' loops are only allowed within async functions and at the top levels of modules.
Broken Code ❌
Fixed Code ✔️
You have to mark your function with the async
keyword:
TS1107
Jump target cannot cross function boundary.
Broken Code ❌
Solution:
Replace break
with return
to exit the function when the condition is met:
Fixed Code ✔️
TS1109
Expression expected.
Broken Code ❌
Fixed Code ✔️
Any snippet of code that evaluates to a value is an expression. Any snippet of code that performs an action is a statement. We need a statement to throw an error inside:
TS1117
An object literal cannot have multiple properties with the same name in strict mode.
Broken Code ❌
Fixed Code ✔️
We can only have one value per property:
TS1121
Octal literals are not allowed. Use the syntax '
0o0
'.
Broken Code ❌
Solution:
Remove the extra zero to prevent the JavaScript interpreter from treating it as an octal literal.
Fixed Code ✔️
TS1127
Invalid character.
Broken Code ❌
Fixed Code ✔️
Unlike in Python, inline comments cannot begin with a single hash sign (#
) in TypeScript. You must use 2 slashes:
TS1128
Declaration or statement expected.
Broken Code ❌
Fixed Code ✔️
A declaration specifies the data and a statement specifies some action with that data:
TS1149
File name differs from already included file name only in casing.
Broken Code ❌
This error occurs when you import the same file in two different files using two different casing styles (ex. camelCase and UpperCamelCase):
File A:
File B:
Fixed Code ✔️
The error can be fixed by using the same casing style:
File A:
File B:
Alternatively, you can set forceConsistentCasingInFileNames
to false
in your "tsconfig.json" file:
TS1155
'const' declarations must be initialized.
Broken Code ❌
Fixed Code ✔️
Alternatively you can define a block-scoped local variable:
TS1160
Unterminated template literal.
Broken Code ❌
Fixed Code ✔️
This error is similar to TS1002 but refers to the ending of a template literal. To fix it, we have to close the template literal with an ending `
:
TS6133
Unterminated regular expression literal.
Broken Code ❌
Solution:
Correct the regular expression by removing the unnecessary escape before the 'g
' flag.
Fixed Code ✔️
TS1163
A '
yield
' expression is only allowed in a generator body.
Broken Code ❌
Fixed Code ✔️
You have to remove the yield
keyword from the for-of loop:
Broken Code ❌
Solution:
Add the *
symbol to define the function as a generator-async function, allowing yield
within its body.
Fixed Code ✔️
TS1175
'
implements
' clause already seen.
Broken Code ❌
Solution:
Remove the redundant implements Calculator
since ExtendedCalculator
already extends Calculator
.
Fixed Code ✔️
TS1183
An implementation cannot be declared in ambient contexts.
Broken Code ❌
Fixed Code ✔️
You cannot write a function implementation inside a declaration file (*.d.ts
). You can only declare its signature:
Alternatively, you can write your function implementation inside a source code file (*.ts
).
TS1192
Module '
json5
' has no default export.
Broken Code ❌
Fixed Code ✔️
When you are importing a module with built-in TypeScript declarations and TypeScript tells you that this module does not have a default export, then you can solve this problem by adding "allowSyntheticDefaultImports" to your "tsconfig.json" file and setting it to true
:
Module '
.../logdown
' has no default export.
Broken Code ❌
Fixed Code ✔️
'export *' does not re-export a default.
Broken Code ❌
You have to re-export a default (in this case coming from runWhenReady.ts
):
Fixed Code ✔️
TS1196
Catch clause variable type annotation must be 'any' or 'unknown' if specified.
Broken Code ❌
Fixed Code ✔️
Errors in catch clauses can only be typed with any
or unknown
. If you need a more precise error typing, you can use a type guard as follows:
Video Tutorial
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.
Broken Code ❌
Fixed Code ✔️
TS1208
'index.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.
Broken Code ❌
Fixed Code ✔️
To solve the issue you can turn off "isolatedModules" in your "tsconfig.json". If you want to keep going with isolated modules, then you have to add an import or export to your code:
TS1218
Export assignment is not supported when '--module' flag is 'system'.
Broken Code ❌
Fixed Code ✔️
TS1219
Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option in your 'tsconfig' or 'jsconfig' to remove this warning.
Broken Code ❌
Fixed Code ✔️
Simply set "experimentalDecorators" to true
in your "tsconfig.json" file. As long as decorators are experimental you will also have to install the reflect-metadata package to shim the upcoming Metadata Reflection API for ECMAScript. For proper functionality the "emitDecoratorMetadata" option should also be set to true
.
TS1225
Cannot find parameter '
error
'.
Broken Code ❌
Fixed Code ✔️
A type predicate needs a parameter to validate:
TS1228
A type predicate is only allowed in return type position for functions and methods.
Broken Code ❌
Fixed Code ✔️
You have to separate the argument list from the return type definition by a :
:
TS1232
An import declaration can only be used at the top level of a namespace or module.
Broken Code ❌
Fixed Code ✔️
TS1240
Unable to resolve signature of property decorator when called as an expression. The runtime will invoke the decorator with 2 arguments, but the decorator expects 3.
Broken Code ❌
The problem is that the CatchError
decorator is defined as a method decorator but is being used as a property decorator.
Fixed Code ✔️
To fix this issue, turn the method into a property by removing the function
keyword:
TS1241
Unable to resolve signature of method decorator when called as an expression.
Broken Code ❌
This code throws an error because the decorator CatchError
is missing the correct signature for the PropertyDescriptor
.
Fixed Code ✔️
To fix this issue, update the signature for the decorator to include the _propertyKey
parameter:
With this adjustment, the CatchError
decorator now properly resolves the method signature, allowing you to catch and log errors as intended.
TS1243
'static' modifier cannot be used with 'abstract' modifier.
Broken Code ❌
You cannot define a function that is static
and abstract
. You have to keep it static
:
Fixed Code ✔️
'async' modifier cannot be used with 'abstract' modifier.
Broken Code ❌
Fixed Code ✔️
TS1244
Abstract methods can only appear within an abstract class.
Broken Code ❌
Fixed Code ✔️
TS1247
A type literal property cannot have an initializer.
Broken Code ❌
Fixed Code ✔️
The getName
function expects an object literal. However, in TypeScript, object literals are not allowed to have initializing functions or values. To work around this limitation and make use of an initializer, we can convert the object literal into a collective type:
TS1248
A class member cannot have the '
const
' keyword.
Broken Code ❌
Fixed Code ✔️
When inheriting from a base class, you have to use modifiers like readonly
if you want your class members to become constant:
TS1254
A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.
Broken Code ❌
Fixed Code ✔️
TS1259
Module can only be default-imported using the 'esModuleInterop' flag
Broken Code ❌
Fixed Code ✔️
Note: You can enable the 'esModuleInterop' flag also via the CLI:
TS1274
'
in
' modifier can only appear on a type parameter of a class, interface or type alias.
Broken Code ❌
Solution:
Remove the in
and out
modifiers from the function type parameter.
Fixed Code ✔️
TS1280
Namespaces are not allowed in global script files when
verbatimModuleSyntax
is enabled. If this file is not intended to be a global script, setmoduleDetection
toforce
or add an emptyexport {}
statement.
Broken Code ❌
Fixed Code ✔️
Use a quotes around the name global
and turn it into an ambient module by using the declare module
syntax:
TS1308
'await' expressions are only allowed within async functions and at the top levels of modules.ts.
Broken Code ❌
Fixed Code ✔️
You cannot use an await
expression in an useEffect
hook, but you can use legacy Promise calls:
Alternatively, you can use an IIFE (Immediately-invoked Function Expression) in your useEffect
hook:
TS1309
TS1309: The current file is a CommonJS module and cannot use 'await' at the top level.
Broken Code ❌
Solution:
This error occurs because top-level await
is only supported in ECMAScript modules, not in CommonJS modules which are the default module system in Node.js unless specified otherwise.
To use top-level await
, you need to configure your project to use ECMAScript modules by adding "type": "module"
to your package.json
. This tells Node.js to treat all code files as ECMAScript modules.
Fixed Code ✔️
Another Solution:
To work around the restriction of not using await at the top level in a CommonJS module, you can also encapsulate your asynchronous operations within an Immediately Invoked Function Expression (IIFE) that is async:
TS1323
Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node16', or 'nodenext'.
Broken Code ❌
Fixed Code ✔️
Dynamic imports are expressions that enable the loading of an ECMAScript module (ESM) into CommonJS modules (CJS).
The import()
expression also allow modules to be loaded conditionally on demand. This feature was introduced with the ECMAScript® 2020 Language Specification.
If your project uses an older module system, remember to update the module
setting in your TypeScript compiler settings.
TS1337
An index signature parameter type cannot be a literal type or generic type. Consider using a mapped object type instead.
Broken Code ❌
Fixed Code ✔️
Alternative:
An index signature parameter type cannot be a union type. Consider using a mapped object type instead.
Broken Code ❌
Fixed Code ✔️
Solution with mapped object type:
Alternative:
TS1341
Class constructor may not be an accessor.
Broken Code ❌
Fixed Code ✔️
You can't name accessors as constructor
:
TS1345
An expression of type '
void
' cannot be tested for truthiness.
Broken Code ❌
Solution:
This error occurs because the assertPositiveNumber
function is an assertion function and does not return a value (its return type is void
). You should call it directly to perform the assertion and then proceed with the divide
function if no error is thrown.
Fixed Code ✔️
TS1355
A 'const' assertions can only be applied to references to enum members, or string, number, boolean, array, or object literals.
Broken Code ❌
Fixed Code ✔️
You cannot apply a const assertion to the result of a ternary operator. It is necessary to restrict the usage of const assertions to the individual literal expressions:
TS1357
An enum member name must be followed by a '
,
', '=
', or '}
'.
Broken Code ❌
Fixed Code ✔️
TS1361
'
Range
' cannot be used as a value because it was imported using 'import type'.
Broken Code ❌
Fixed Code ✔️
The type Range
is being used to declare the return type of static function Ranger.getFullRange
but later on it is also being used to create an instance using new Range
. When you want to use a type to construct instances (or do anything else beyond declaring types), then you have to use the import
statement instead of import type
:
TS1363
A type-only import can specify a default import or named bindings, but not both.
Broken Code ❌
Fixed Code ✔️
With type-only imports and exports you cannot use a default import together with a named import in one single line of code. The TypeScript team chose this limitation to avoid ambiguity. You will have to use separate import statements:
TS1368
Class constructor may not be a generator.
Broken Code ❌
Fixed Code ✔️
You can't name generators as constructor
:
TS1371
This import is never used as a value and must use 'import type' because 'importsNotUsedAsValues' is set to 'error'.
Broken Code ❌
Fixed Code ✔️
Solution 1:
When "importsNotUsedAsValues" is set to "error" in your "tsconfig.json", then you have to use the "import type" syntax when you just want to refer to a type instead of using it as a value:
Solution 2:
Use the class also as a value and not just a type:
Solution 3:
You can also set "importsNotUsedAsValues" to "preserve" which is not recommended.
TS1375
'await' expressions are only allowed at the top level of a file when that file is a module, but this file has no imports or exports. Consider adding an empty 'export {}' to make this file a module.
Broken Code ❌
Fixed Code ✔️
TS1378
Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.
Broken Code ❌
Fixed Code ✔️
TS1385
Function type notation must be parenthesized when used in a union type.
Broken Code ❌
Fixed Code ✔️
When using a union type, you have to put additional function types in parentheses:
TS1389
'this' is not allowed as a variable declaration name.
Broken Code ❌
Fixed Code ✔️
The name this
cannot be used to declare a variable because it is already a reserved keyword to refer to the current object in a method or constructor.
That's why you have to choose a different name:
TS1431
'for await' loops are only allowed at the top level of a file when that file is a module, but this file has no imports or exports. Consider adding an empty 'export {}' to make this file a module.
Broken Code ❌
Fixed Code ✔️
Just add the imports for the fs
module and readline
module in order to turn your code into a module itself:
TS1432
Top-level 'for await' loops are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', or 'nodenext', and the 'target' option is set to 'es2017' or higher.
Broken Code ❌
Fixed Code ✔️
The error is similar to TS1378 and needs an adjustment in the tsconfig.json
file:
TS1434
Unexpected keyword or identifier.
Broken Code ❌
Fixed Code ✔️
You have to remove the duplicate static
keyword:
TS1450
Dynamic imports can only accept a module specifier and an optional set of attributes as arguments.
Broken Code ❌
This error occurs because the TypeScript compiler expects a dynamic import as the imported module is assigned to a constant.
Fixed Code ✔️
To fix this error, use an import statement to indicate to the compiler that a module is being imported:
TS1470
The '
import.meta
' meta-property is not allowed in files which will build into CommonJS output.
Broken Code ❌
Solution:
Since import.meta
is not allowed when targeting CommonJS, you need to use an alternative method to determine the directory path. In a CommonJS environment, you can use the built-in __dirname
global variable directly without needing to calculate it from __filename
.
Fixed Code ✔️
TS1471
Module '
@headlessui/react
' cannot be imported using this construct. The specifier only resolves to an ES module, which cannot be imported synchronously. Use dynamic import instead.
Broken Code ❌
Fixed Code ✔️
Using a dynamic import:
TS1479
The current file is a CommonJS module whose imports will produce '
require
' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require
'. Consider writing a dynamic 'import()
' call instead.
Broken Code ❌
Solution:
To import an ECMAScript module in a CommonJS module, you can use a dynamic import which works asynchronously and returns a Promise.
Fixed Code ✔️
TS1484
ContentCollectionKey
is a type and must be imported using a type-only import whenverbatimModuleSyntax
is enabled.
Broken Code ❌
Fixed Code ✔️
Type-only imports can be made using the type
keyword in front of your type's name:
TS2300
Duplicate identifier 'name'.
Broken Code ❌
Objects don't support multiple properties with the same name:
Fixed Code ✔️
To fix the error we have to remove the duplicated property:
TS2304
Cannot find name 'world'.
Broken Code ❌
Fixed Code ✔️
It can happen that TypeScript does not know about your global objects because those may be injected from an unknown runtime environment or third-party JavaScript library. The easiest way to let TypeScript know about this is to declare the ambience (ambient context):
Cannot find name 'Promise'
Broken Code ❌
Fixed Code ✔️
Install es6-promise
type definitions with the typings tool.
Adding the following line to the beginning of every file using definitions from es6-promise
.
Cannot find name 'Promise'
Broken Code ❌
Fixed Code ✔️
Cannot find name 'FC'.
Broken Code ❌
Fixed Code ✔️
TS2305
Module '
./sum
' has no exported member 'multiply
'.
Broken Code ❌
Fixed Code ✔️
The file "sum.ts" exports a function named "sum", so we have to fix the named import in our "main.ts" file:
TS2306
File '
add.ts
' is not a module.
Broken Code ❌
Fixed Code ✔️
The error TS2306 signals that the file (add.ts
) can be found (otherwise it would throw error TS2307) but does not provide the necessary exports. We can solve this with a named export:
Alternatively, we can use a default export:
Using a default export requires that we also adjust our import statement in main.ts
(otherwise we would end up with error TS2614):
Video Tutorial
TS2307
Cannot find module 'events' or its corresponding type declarations.
Broken Code ❌
You are importing from a core Node.js module (e.g. event
) without having Node.js type definitions installed:
Fixed Code ✔️
Import Node.js type definitions first in order to use Node.js core modules:
Read More: Cannot find module events
TS2314
Generic type '
Omit
' requires 2 type argument(s).
Broken Code ❌
Fixed Code ✔️
When using the Omit
utility type, you have to list property overwrites with a pipe (|
):
Generic type '
ReadonlyArray
' requires 1 type argument(s).
Broken Code ❌
Fixed Code ✔️
When using a generic (in this case ReadonlyArray<T>
), then you have to pass a type argument to it:
TS2315
Type '
CustomRequest
' is not generic.
Broken Code ❌
Fixed Code ✔️
When supplying a type (recognizable by the use of the diamond operator <>
), then we have to make sure that our type actually supports generics to capture the type that we provide:
TS2318
TS2318: Cannot find global type '
AsyncIterableIterator
'.
Broken Code ❌
Solution:
This issue is often related to the TypeScript configuration not including the necessary libraries that define modern JavaScript features like AsyncIterableIterator
. To ensure that TypeScript supports Symbol.asyncIterator
properly, your TypeScript configuration needs to either target esnext
or explicitly include the esnext
.asynciterable library if you're using a targeted subset of features from ECMAScript proposals.
Fixed Code ✔️
TS2322
Type 'string' is not assignable to type 'number'.
Broken Code ❌
Fixed Code ✔️
The type of the returned value must match the return type specified in the function signature:
Video Tutorial
TS2335
'super' can only be referenced in a derived class.
Broken Code ❌
Fixed Code ✔️
Your derived class has to "extend" the base class:
TS2339
Property '
width
' does not exist on type 'Shape
'.
Broken Code ❌
Fixed Code ✔️
You can create discriminated unions by sharing a single field (e.g. kind
) in your type definitions and using a union type in connection with a switch-case statement that helps the TypeScript compiler to distinguish the different types:
Property '
pop
' does not exist on type 'readonly [1, 2, 3]
'.
Broken Code ❌
When using a const assertion on an array, then your array becomes readonly (immutable), so you cannot modify its elements using in-place operations such as pop
. You will have to make your array mutable:
TS2341
Property '
startSimulation
' is private and only accessible within class 'ExchangeMock
'.
Broken Code ❌
Solution:
Access the private property startSimulation
using bracket notation, which is a workaround to access private members from outside the class in TypeScript.
Fixed Code ✔️
TS2344
Type '
{ name: string; }
' does not satisfy the constraint '{ age: number; }
'. Property 'age
' is missing in type '{ name: string; }
' but required in type '{ age: number; }
'.
Broken Code ❌
Fixed Code ✔️
The "constraint" in this context is set by the T extends
statement. When passing a type argument, the required properties of the type variable (T
) must be matched:
As an alternative, type argument inference can be used:
TS2345
Argument of type '
number
' is not assignable to parameter of type 'TimerHandler
'.
Broken Code ❌
Fixed Code ✔️
There is a mismatch in the expected arguments of a function. The setTimeout
function expects the first argument to be a callback function and not the returned value (in this case a number
) of a function call:
Video Tutorial
TS2348
Value of type 'typeof BaseN' is not callable. Did you mean to include 'new'?
Broken Code ❌
Fixed Code ✔️
TS2349
Cannot invoke an expression whose type lacks a call signature. Type 'Promise
' has no compatible call signatures.
Broken Code ❌
Fixed Code ✔️
TS2350
Only a void function can be called with the '
new
' keyword.
Broken Code ❌
This error occurs because NextResponse.json()
is a static method and not a constructor. Therefore, it shouldn't be used with the new
keyword.
Fixed Code ✔️
Remove the new
keyword when calling NextResponse.json()
:
This way, you're correctly invoking the static method without trying to instantiate it.
TS2351
This expression is not constructable. Type '
EMA
' has no construct signatures.
Broken Code ❌
Fixed Code ✔️
TS2352
Conversion of type '
{ name: string; age: number; }
' to type 'Person
' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown
' first.
Broken Code ❌
Fixed Code ✔️
Make sure all properties of your object match the properties of your declared type:
Alternative but not recommended: Convert your object to unknown
first:
TS2353
TS2353: Object literal may only specify known properties, and '
id
' does not exist in typeOmit<VideoEntity, 'id'>
.
Broken Code ❌
Solution:
In the broken code, you are trying to set the id
property on an instance of VideoEntity
where id
has been explicitly removed from the permissible properties through the use of Omit<VideoEntity, 'id'>
.
Remove the id
property from the object literal since it has been omitted from the type definition you are trying to conform to.
Fixed Code ✔️
TS2355
A function whose declared type is neither 'void' nor 'any' must return a value.
Broken Code ❌
Fixed Code ✔️
TS2361
The right-hand side of an 'in' expression must not be a primitive.
Broken Code ❌
Fixed Code ✔️
You cannot use the in
keyword on primitive data types. That's why we have to replace the primitive type unknown
with a non-primitive type like object
:
An alternative solution would be to use a type assertion on the right-hand side of the 'in' expression:
TS2362
The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
Broken Code ❌
Fixed Code ✔️
When you want to merge two objects, you have a multitude of possibilities:
Spread Syntax
Object.assign
TS2364
The left-hand side of an assignment expression must be a variable or a property access.
Broken Code ❌
Fixed Code ✔️
Using a variable on the left-hand side assignment:
TS2365
Operator '+' cannot be applied to types 'number' and 'object'.
Broken Code ❌
Fixed Code ✔️
You can use the +
operator only with equivalent data types (strings + strings or numbers + numbers):
TS2366
Function lacks ending return statement and return type does not include 'undefined'.
Broken Code ❌
Fixed Code ✔️
The switch-case statement isn't handling all cases from every possible input. We can solve that by defining a default
case:
Another solution would be to implement the missing case for 3
:
Video Tutorial
TS2367
This condition will always return 'false' since the types '{ message: string; }[] | undefined' and 'number' have no overlap.
Broken Code ❌
Fixed Code ✔️
An array cannot be 0
, so doing a check for equality with 0
makes no sense. What may be useful instead is checking the array length:
TS2368
Type parameter name cannot be 'number'.
Broken Code ❌
Fixed Code ✔️
The easiest way to fix the error is to make age
optional as well:
TS2369
A parameter property is only allowed in a constructor implementation.
Broken Code ❌
Fixed Code ✔️
The constructor implementation is missing curly brackets which is why TypeScript does not recognize the constructor implementation and files error TS2369. To solve it you have to add the missing curly brackets:
TS2370
A rest parameter must be of an array type.
Broken Code ❌
Fixed Code ✔️
A rest parameter allows a function to accept an indefinite number of parameters. To signal that it can be multiple values, we have to use an array
type for our rest parameter:
TS2371
A parameter initializer is only allowed in a function or constructor implementation.
Broken Code ❌
Fixed Code ✔️
Remove parameter initializer from function overload:
TS2372
Parameter '
score
' cannot reference itself.
Broken Code ❌
Fixed Code ✔️
If you want to use a default value for a parameter, then you have to set it to a fixed value instead of referencing the parameter to itself:
TS2377
Constructors for derived classes must contain a 'super' call.
Broken Code ❌
Fixed Code ✔️
Every constructor
in a derived class has to call the super
method to invoke the constructor
of the base class. It has to be the very first call:
Video Tutorial
TS2378
A 'get' accessor must return a value.
Broken Code ❌
Fixed Code ✔️
TS2383
TS2383: Overload signatures must all be exported or non-exported.
Broken Code ❌
Solution:
This TypeScript error occurs because the function overloads for isTuple
are internally defined (not exported), but the implementation signature is exported. This discrepancy between the visibility of the overload signatures and the implementation signature leads to the error.
To fix this error, you need to ensure consistent export status for all overload signatures and the function implementation. You can either export all the overload signatures or keep them all internal. In most cases, if you intend to use the function outside the module, you should export everything.
Fixed Code ✔️
TS2389
Function implementation name must be '
getOpenPosition
'.
Broken Code ❌
Solution:
Rename getOrder
to getOpenPosition
to match the declared method signatures.
Fixed Code ✔️
TS2390
Constructor implementation is missing.
Broken Code ❌
Fixed Code ✔️
A constructor implementation must be put in curly brackets:
TS2391
Function implementation is missing or not immediately following the declaration.
Broken Code ❌
Fixed Code ✔️
An abstract class is different from an interface. You have to use the abstract
modifier if you want to define a contract in an abstract class. If there is no abstract
modifier you will have to provide a implementation.
Solution 1:
To solve the problem, we can mark makeNoise
with the abstract
keyword. That will enforce derived classes to implement this method on their own:
Solution 2:
Another solution is to provide a base implementation for makeNoise
:
Video Tutorial
TS2393
Duplicate function implementation.
Broken Code ❌
Fixed Code ✔️
This error may occur when you have multiple files within the same project that implement the same function with the same name. To resolve this issue, you need to configure TypeScript to recognize a file as a script rather than a module. This can be achieved by setting the "moduleDetection" option to "force" instead of the default "auto":
TS2394
This overload signature is not compatible with its implementation signature.
Broken Code ❌
The implementation does not match all signatures:
Fixed Code ✔️
To match the first function overload, we have to add code to our function body which can also return a number
:
Video Tutorial
TS2395
Individual declarations in merged declaration '
React
' must be all exported or all local.
Broken Code ❌
Fixed Code ✔️
Make sure to export your additional declaration:
TS2397
Declaration name conflicts with built-in global identifier '
globalThis
'.