question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[RFC] TypeScript migration

See original GitHub issue

Summary

TypeScript is a superset of Javascript – all Javascript is valid Typescript. However, once a project has typescript enabled, you can annotate your code to provide type hints to your users and other developers.

Just as shared styles between Carbon frameworks improves user experience, so to would shared behavior. Typesafety allows us to ship components that perform and behave as expected.

Related issues: #1296, #727, #1103, #580

Motivation

End users, product developers, as well as Carbon developers would all benefit from the addition of type safety to Carbon products

  • End Users – by shipping more resilient, bug free components end users will encounter fewer unexpected edge cases
  • Product developers – developers would be guided towards proper API usage and would be alerted to new or changed apis.

JS Example One

JS Example Two

  • Carbon developers and Typescript developers
  • Fewer Uncaught TypeError(Cannot read x of undefined or spelling errors means more time on real issues.
  • Fewer undefined refs and instance properties
  • Reduce reliance on null and undefined guards means more resiliant APIs – Alerted to misuse of props, state, third-party library, and dom errors while writing your code.

TS Example

Design

There are currently to main ways to compile typescript into javascript(the typescript compiler, and through babel. Since our project is already using Babel, switching to typescript would be unnecessary.

Namespaces and const enums are not supported by the babel compilation, however, this can be mitigated with compiler configuration.

As we look toward migrating codebases to a mono-repo, typesafety will offer a means to share types between projects and permit large scale refactors with confidence.

Drawbacks

  • Time spent onboarding carbon devs to typescript
    • I volunteer as tribute
  • Friction to carbon contributors new to the project
    • would empower them to make more meaningful changes
    • would reduce friction for folks onboarding to product teams

Alternatives

Non-Typescript

  • FlowType
    • call-site type checking is nice
    • Lots of projects switching to TS(Jest, Expo, Atlassian, Yarn.
    • Far fewer third party integrations 1/10th of the questions on SO
  • JSDoc
    • some bugs around object types
    • errors in jsdoc means no warnings/errors it just won’t get annotated

TypeScript without migration

These solutions offer some of the benefits of TypeScript types with different drawbacks. While the core library would still be standard JavaScript maintaining types would be a manual effort.

The main issue with these strategies is that they require a much more manual effort up front and the types would be of lower quality than those generated natively from the project in situ.

Definitely-typed

  • Types published using the DefinitelyTyped repo https://github.com/DefinitelyTyped/DefinitelyTyped
  • Recommended solution from TS team for no TypeScript projects
  • Rely on community involvement
  • Issues keeping types in parity with new library features and changes
  • We don’t benefit from the types internally

Internal .d.ts files

Adoption strategy

At no point will typescript migration stop or prevent work on the project. TypeScript can get out of the way when necessary (@ts-ignore and any or unknown types). It will never break builds unless we chose to enable that behavior.

While each of the Carbon javascript projects would benefit from the addition of types. I believe the most valuable library at this time would be carbon components react (CCR).

Since we already have some run-time type checking through prop-types, we already have a starting point. In addition, thanks to prop-types CCR developers are already at least slightly familiar with the constraints imposed by types.

Conversion path

A – Just rename everything to .ts and .tsx

  1. The first pass at converting the project will rename the files and add babel, storybook, and jest configuration to handle these new file types.

  2. The initial config will have the loosest possible config. Separate PR’s will be made to turn on each of the strict-mode rules separately resolving issues as they come up.

  3. Any objective errors uncovered as a result of the conversion will be resolved as part of the same PR.

  4. Prop types will be defined at the same level of specificity as the prop-types. No unnecessary types added.

  5. Types can be shipped from day one

B – Leave the files as .js config enable allowJS and checkJS

  1. Recommended by TypeScript team for large projects using the TypeScript compiler (likely not us)
  2. All new code should be written as .ts
  3. Gradually migrate files to .ts
  4. Types can be shipped once all .js files are converted to TypeScript

Migration stories from others

  • hootsuite: Thoughts on migrating to TypeScript
  • clayallsop: Incrementally-migrating-javascript-to-typescript-565020e49c88 'Incrementally Migrating JavaScript to TypeScript
  • pleo: Migrating a Babel project to TypeScript
  • mstsreactconversionguide: TypeScript React Conversion Guide
  • entria: Incremental Migration to TypeScript on a Flowtype codebase

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:11
  • Comments:20 (11 by maintainers)

github_iconTop GitHub Comments

5reactions
michaeljotacommented, Sep 22, 2020

Do you have any updates on this? I think moving into TS will really improve the developer experience, including your own.

4reactions
kalbert312commented, Aug 7, 2019

Moved my typings to a definitelytyped PR: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/37442

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ankush Dharkar - [RFC] Migrate backend to TypeScript
Hey Folks, we are going to migrate our backend from nodeJS to TypeScript at Real Dev Squad. The codebase is medium-sized and a...
Read more >
TypeScript Adoption Plan - Ember RFCs
RFCs which deprecate existing APIs must consider the migration path for TypeScript users as well as JavaScript users.
Read more >
Migrate From V6 to V7 - Simple React forms validation
Upgrade Packages. Important: if you are using TypeScript, react hook form will need to have TS 4.3 above.
Read more >
Migrating to AWS CDK v2 - AWS Documentation - Amazon.com
For full details, see the RFC . ... For TypeScript developers, TypeScript 3.8 or later is required. ... Migrating from AWS CDK v1...
Read more >
This Week In React #120: use RFC, Sandpack, Progressive ...
We could even think that after the migration to the new architecture and the stabilization of public APIs, there could be a 1.0...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found