Typescript

The whole value of Typescript in being able to define code that is type-safe, and have any non-type safe code to be uncovered at compile time. This inevitably saves us a large amount of effort, since we don't have to wait until runtime to be able to catch these bugs. It makes our code tighter, easier to reason about and more robust.

  • Generics is one way to implement this when writing functions

Instead of running our javascript files directly as we would in a Javascript project, we run our js files through the Typescript compiler, and we run the output it produces.

TypeScript’s type system is very powerful because it allows expressing types in terms of other types. We can accomplish this via:

Naturally, any code that does not exist as far as Javascript is concerned (e.g. Generics, Interfaces) does not add to the runtime cost of the code.

non-null assertion

Non-null assertion operator shouldn't be used unless you have a very good reason to do so

  • Non-null assertion operators are simply a way of telling the compiler, "trust me, I’m absolutely sure that this optional value is in fact never empty."
  • While you might think that a value is always non-empty, you could be wrong. Or worse, you could be correct at the moment, but it will not be the case in the future, because some other developer will call your function with a null or undefined.
function sayHello(person: Person | undefined) {
    person!.hello(); // no errors!
}

The operator could come in handy, but generally our feeling for it should be that if we are reaching for it, there is probably a more correct way to do it.

  • sometimes the overhead of the type system is not justified, and we just want to create a quick escape hatch that makes sense. We have to address these case by case.

Another situation in which it might make sense to use this operator is when you want to enable strict null checks on a large, existing codebase. Enabling the flag in such a case might result in hundreds or thousands of errors. It’s a huge effort to go through them all at once. On the other hand, you cannot merge your changes to the master if the whole project doesn’t compile successfully. In this case, the non-null assertion operator might come in handy, as it will allow you to quickly suppress the errors that you don’t have time to fix properly.

Compiler

We can use ESBuild to compile Typescript on server side


Children
  1. Compiler
  2. Cook
  3. Declaration File
  4. Declaration Merging
  5. Examples
  6. Lang
  7. Modules
  8. Overloading
  9. Patterns
  10. TS Config
  11. Type
  12. Types

Backlinks