/errors/typescript/ts2322-type-is-not-assignable-to-type
error TS2322: Type 'string' is not assignable to type 'number'
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · v24.20.0updated
- tool
- TypeScript
- exit code
- 2
- versions
- TypeScript 5.x
- os
- any
error TS2322: Type 'A' is not assignable to type 'B' means TypeScript found a value of type A where the declared type is B, and A is not compatible with B. TypeScript never converts values for you: convert the value (Number(input)), narrow it with a check, or correct the type annotation if it is the annotation that is wrong.
Reproduce it
npm init -y >/dev/null
npm install --silent --no-audit --no-fund typescript@5 >/dev/null 2>&1
cat > app.ts <<'EOF'
const input: string = "8080";
let port: number = input;
interface User { id: number; name: string }
const user: User = { id: "1", name: "Ada" };
function parsePort(value: string | undefined): number {
return value;
}
EOF
npx tsc --noEmit --strict --pretty false app.tsOutput · exit 2
app.ts(2,5): error TS2322: Type 'string' is not assignable to type 'number'. app.ts(5,22): error TS2322: Type 'string' is not assignable to type 'number'. app.ts(8,3): error TS2322: Type 'string | undefined' is not assignable to type 'number'. Type 'undefined' is not assignable to type 'number'.
The location app.ts(2,5) is line and column. For object literals the error points at the property and names the expected type from the interface; for return values it names the declared return type.
Fixes
npm init -y >/dev/null
npm install --silent --no-audit --no-fund typescript@5 >/dev/null 2>&1
cat > app.ts <<'EOF'
const input: string = "8080";
let port: number = Number(input);
interface User { id: number; name: string }
const user: User = { id: Number("1"), name: "Ada" };
function parsePort(value: string | undefined): number {
if (value === undefined) return 3000; // narrowing removes undefined
return parseInt(value, 10);
}
console.log(port, user.id, parsePort(undefined), parsePort("8443"));
EOF
npx tsc --strict --pretty false app.ts && node app.jsOutput
8080 1 3000 8443
Sources
Related pages
errors · typescriptTS2307: Cannot find module 'x' or its corresponding type declarationstsc reports TS2307 when an import resolves to no file and no type declaration. Install the package and its @types, fix the path, or declare the module.config · typescripttsconfig strict: what "strict": true enablesThe strict option in tsconfig.json enables noImplicitAny, strictNullChecks and the other strict checks at once. It defaults to true since TypeScript 6.0.errors · javascriptTypeError: x is not a function (JavaScript)JavaScript throws x is not a function when you call a value that is undefined, an object or a string: a typo, a missing import, or .map on a non-array. Fixes.commands · typescripttsc --noEmit: type check a TypeScript project without emitting filesnpx tsc --noEmit type checks a TypeScript project without writing .js files. Exit codes, watch and incremental modes, and how to add it to CI and npm scripts.