Skip to main content
CodingMarble

Web development

TypeScript vs JavaScript for Beginners: Which First?

Learn JavaScript first, then add TypeScript once you can build small projects without copying code blindly. TypeScript adds static types and better editor help, but it is removed before your code runs, so it never replaces testing or input validation.

By CodingMarble · Published · 5 min read

Watercolour wireframe sketches of website layouts
Photo: Hal Gatewood on Unsplash

What TypeScript is, in one paragraph

TypeScript is JavaScript with an optional layer of type annotations on top. You write files ending in .ts, the compiler checks that your values are used consistently, and then it strips the annotations and outputs ordinary JavaScript that browsers and Node.js run. Every valid JavaScript program is also a valid TypeScript program in spirit, so learning TypeScript is not learning a second language from zero. It is learning to describe the data your JavaScript already handles.

What TypeScript adds

The first gain is static checking. The compiler reads your code without running it and reports mistakes such as calling a function with the wrong kind of argument, misspelling a property name, or forgetting that a value might be undefined. These are errors that plain JavaScript would only reveal at runtime, often on a page you did not test.

The second gain is editor support. Because the editor knows the shape of every object, autocomplete suggests real property names, hovering shows signatures, and renaming a function updates every use safely. For a beginner working inside a large library or framework, this acts like built-in documentation. The third gain is that types document intent: a function signature tells the next reader what goes in and what comes out without reading the body.

What TypeScript does not do

Types exist only while you are writing and compiling. At runtime there are no types at all, only JavaScript. That has a consequence people learn the hard way: if you annotate the result of a network request as a User, the compiler believes you, but the server can still send something different, and nothing will complain until the code breaks. TypeScript cannot protect you from bad data arriving from outside your program.

For data from forms, URLs, APIs and databases you still need runtime validation, using a schema library or hand-written checks, and you still need tests. Think of TypeScript as a very good proofreader of your own code, not a guard at the door. It also does not make code faster, and it does not remove the need to understand JavaScript behaviour such as closures, asynchronous code and how this works.

A tiny before and after

Here is a classic JavaScript slip. A function totals prices, and a caller passes a string it read from an input field. JavaScript quietly concatenates instead of adding, and the bug surfaces later as a strange number on screen.

JavaScript· JavaScript: runs without complaint and gives a wrong answer
function addTax(price, rate) {
  return price + price * rate;
}

console.log(addTax("100", 0.18)); // "10018", not 118

In the example above, "100" * 0.18 is 18 because multiplication converts the string, but the leading price is added as text, so the result is the string "10018". The page shows a nonsense value and no error is raised. With a type annotation, the mistake is flagged before the code ever runs.

TypeScript· TypeScript: the compiler rejects the call at build time
function addTax(price: number, rate: number): number {
  return price + price * rate;
}

// Error: Argument of type 'string' is not assignable
// to parameter of type 'number'.
console.log(addTax("100", 0.18));

// Fixed: convert deliberately, and check the result.
const price = Number("100");
console.log(addTax(price, 0.18)); // 118

Notice that the fix is a conscious conversion. TypeScript forced a decision at the exact line where the types disagreed. Also notice what it did not do: if the user typed abc, Number returns NaN and the type system is still satisfied, because NaN is a number. Runtime checks remain your job.

A sensible learning order

Start with JavaScript until the language stops feeling like magic. Practise variables, functions, arrays and objects, array methods such as map and filter, promises with async and await, and talking to the DOM or to an API. Free references such as MDN Web Docs and javascript.info cover all of this. The goal is not to finish a course but to build two or three small projects where you understand every line.

Then add TypeScript in a project you already know. Because the logic is familiar, the new thing you are learning is only the types, which is far easier than learning both together. A reasonable order is basic annotations, then interfaces and type aliases, then union types and narrowing, then generics when you meet them in real code, and finally utility types. The official TypeScript handbook follows this progression.

There is one honest exception. If you are following a framework tutorial or a job path where the whole ecosystem uses TypeScript by default, you may meet it on day one. That is fine as long as you keep reading errors as information and do not silence them with the any type to make the red lines disappear.

Strictness settings and migrating a project

TypeScript behaviour is controlled by a tsconfig.json file. The most important setting is strict, which switches on a group of checks including strictNullChecks, the one that catches code that forgets a value can be null or undefined. Starting a new project with strict enabled is much easier than turning it on later, because the compiler never lets bad habits form.

JSON· A minimal tsconfig.json for a new project
{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ESNext",
    "strict": true,
    "noEmit": true,
    "allowJs": true,
    "checkJs": false
  },
  "include": ["src"]
}

To migrate an existing JavaScript project, do it in steps instead of rewriting. Add a tsconfig with allowJs set to true so both file types coexist. Rename one file at a time from .js to .ts, starting with small utility modules that many files import. Fix the errors in that file, commit, and move on. Third-party libraries usually ship their own types or have them in the DefinitelyTyped packages; check the library documentation for the current way to install them. Resist adding any everywhere; use unknown for values you cannot describe yet, because it forces a check before use.

Practical tip: when the compiler error looks unreadable, read it from the bottom line upward. The last line usually states the real mismatch, and the lines above explain where the types were inferred from.

When plain JavaScript is fine

Plain JavaScript remains a good choice for small scripts, prototypes you plan to throw away, simple landing-page interactions, and learning exercises. A build step and configuration file are overhead that a fifty-line script does not need. Editors can also give useful hints for JavaScript through JSDoc comments if you want light checking without converting files.

TypeScript earns its cost as projects grow: more files, more contributors, longer lifetimes, and data structures that many parts of the code share. If you expect to maintain the code for a long time or hand it to someone else, the types become part of the documentation. If you are unsure, start in JavaScript, and move to TypeScript the first time you catch yourself scrolling up to remember what shape an object has.

Need this built properly for your business?

CodingMarble is a remote studio in India that builds websites, apps and games on fixed packages with the scope written down before payment. Try the free tools we run, or start a project when you are ready.