Sub topic 4
Rename the File from `.js` to `.ts`
This chapter assumes you know modern JavaScript, can run Node.js and npm commands, and already have a tsconfig.json that includes src. It also assumes you understand that TypeScript checks code before it runs, while Node.js runs JavaScript.
Renaming one file is the smallest useful migration step. The file keeps its behavior, but the TypeScript compiler begins treating it as TypeScript. The compiler can then report unsafe values, missing annotations, and unsupported syntax.
Start with a working JavaScript file
Suppose src/user.js contains this code:
function formatUser(user) {
return user.name.toUpperCase() + " (" + user.age + ")";
}
module.exports = { formatUser };Before changing the extension, run the existing tests or program. This gives you a known working point. A migration is easier to debug when you know whether a problem existed before the rename.
Make sure the file is included by tsconfig.json. For example:
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"rootDir": "src",
"outDir": "dist",
"allowJs": true,
"esModuleInterop": true,
"skipLibCheck": true,
"strict": false
},
"include": ["src"]
}With allowJs enabled, JavaScript and TypeScript files can coexist. This lets you migrate one file without converting the whole project.
Rename the file
Use your file manager or a terminal command:
mv src/user.js src/user.ts
npx tsc --noEmitThe --noEmit option checks the project without creating files in dist. On Windows PowerShell, use this equivalent command:
Rename-Item src/user.js src/user.ts
npx tsc --noEmitThe renamed file is still valid TypeScript because ordinary JavaScript is generally valid TypeScript. However, the compiler now knows that user has no declared type. With strict checking disabled, the compiler might report no error yet. That does not mean the value is safe. It means the current settings allow the missing information.
A useful next version adds an object shape:
interface User {
name: string;
age: number;
}
function formatUser(user: User): string {
return user.name.toUpperCase() + " (" + user.age + ")";
}
module.exports = { formatUser };An interface is a named description of an object’s properties. The User interface requires a string name and a numeric age. The return annotation says that formatUser always returns a string.
Triage the first errors
Treat compiler errors as a migration queue. Fix errors in the renamed file first, then check its callers.
| Error pattern | Likely cause | First response |
|---|---|---|
| Parameter implicitly has type `any` | A parameter has no known type | Add an annotation or infer a type from a shared definition |
| Property does not exist | The object shape is incomplete or incorrect | Update the interface or fix the property access |
| Cannot find module | Import syntax or declaration files are missing | Check the path and package types |
Exercise
Why should you run the existing tests before renaming user.js to user.ts?
Show answer
The tests establish that the JavaScript version worked, so you can distinguish pre-existing failures from migration-related failures.