Skip to content

cosee/techtalk-typescript-2021-02

Repository files navigation

Cosee TechTalks: Strongly Typed - TypeScript in frontend development

After 12 years of web-development, I started as fullstack developer at Cosee about two years ago. In my team I was first working in the backend, but also showed interest in the React frontend that we were writing. Due to some changes in the team structure, I ended up writing frontend code about one year ago and I had to work my way through tons of JavaScript code in order to implement some features. Often, I wished we had concise type information in the code, just like it is always the case in Java code. But let me illustrate that:

Imagin the following React component:

export const ArticleSummary = ({ article }) => {
  return (
    <div>
      <h1>{article.displaytitle}</h1>
      <p>{article.extract}</p>
      {/*TODO: add thumbnail*/}
    </div>
  );
};

Your task is to add a thumbnail to the page, what would you do? Ask youself, where to get the thumbnail from! Or the URL of the thumbnail. Or some kind of id that can be used to retrieve the thumbnail.

But, this code just does not contain this information. Even the function that retrieves the data, does not contain it. You have to get some real data from the backend. The way to research this is long and painful, depending on the size of your project of course...

You could add type information into js-doc comments and sometimes it is a reflex of mine to write such comments, but in the modern world, TypeScript is a better alternative. You can add type information in a machine-readable way, just like in Java. It serves as documentation, but it also stays up-to-date, because the compiler uses it to validate your code. You cannot "forget to change it"...

And when you use TypeScript, you will notice that the IDE is able to assist you much more than with plain JavaScript. You get auto-completion, refactorings and safety from spelling errors.

In my talk, I present some helpful uses of TypeScript and in this repository you will have all the examples from the talk and some more, that didn't fit into the time-frame

A short note: We are using IntelliJ Idea or Webstorm at work most of the time. Typescript integration works very well there. I am not sure how we auto

The typescript ArticleSummary component and the fetchArticalSummary function are here:

TypeScript - Basics

Simple example of a TypeScript file and the compilation result.

Type definition features

Generate Types from OpenAPI/Swagger

As mentioned in the talk, there are scripts that download and preprocess the OpenAPI spec. Those are written in NodeJS (pure JavaScript, because setting up TypeScript in Node is not that easy)

Example files for openapiClientAxios setup

Type-safe Translation

Example for type-sate translations setup with react-i18next

Runtime-Checks

TypeScript is different to Java in that it does has any type information at runtime. No typechecks are performed when using "any" variables or type-casts with "as".

The following files demonstrate how a composable type-checker can be implemented.

io-ts

"io-ts" is a package that provides runtime-checks similar to the examples above. Personally, I haven't used this package in production yet. The documentation is a bit hard to read and the function programming paradigms are not intuitive for the typical JS/TS-developer. But it may suit your needs.

I dropped it after I didn't succeed in writing a custom codec.

Error analysis

Package numbers (TS vs JS)

In order to analyse the number of TypeScript packages in npm, I created an elastic-search index filled with contents from the npm-registry. The complete setup to do that is here:

https://gitlab.com/nknapp/typescript-pkg-search

TypeScript setup

This project was created with the command

yarn create react-app typescript-react-example --template typescript

See package.json for required packages and scripts.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published