Assume the following:

/// file.ts
var x = "hello";
x = 1;
/// file.ts

function transpile(fileName) {
  1. What steps would typescript take to transpile the file?
  2. How and when the typescript safety check cause the second line to become an invalid assignment? (Edit: in Visual Studio designer this happens real time, is it compiled behind the scene every time a change is made to the file?)


This was an interview question on Node.Js subject and I didn't even think someone might be interested in asking how the TypeScript transpiler works. I do not understand how is it that they never ask about the scale cube or monolithic architecture considering most companies are in the business of commercial software development.

  • Are you asking about how a language translator (or compiler) works, sort of internally, or, whether such an error is detected at compile time vs. runtime? – Erik Eidt May 6 '17 at 0:37

When the TypeScript compiler is invoked, that compiler will read the input, parse it, analyze it, and translate it into JavaScript output.

Analysis will first determine that the variable x has type string, then that a value of numeric type is being assigned to a variable of type string.

These types are incompatible according to the TypeScript language, so an error is produced. (Some output may still be generated, but shouldn't be used as the input is in error.).

So essentially, the error happens at compile time, during the TypeScript compiler's analysis of the input program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.