Small TypeScript improvements

17 views
Skip to first unread message

Tim Van der Lippe

unread,
Dec 14, 2020, 11:00:23 AM12/14/20
to devtools-dev
Hey devtools-dev,

Today two small TypeScript improvements have landed:
1. Return types of functions/method declarations in the `front_end/` are now required by ESLint. This change reflects the internal TypeScript style guide. We are working on enabling this rule for non-`front_end/` code as well.
2. We have added a new function called `assertNever` to Platform (https://crrev.com/c/2589953). This function can be used to enforce exhaustive switch statements during compile time. In the default-clause of a switch statement (given that all values of an enum/union have been handled), TypeScript infers the switched-value to be of type `never`. As such, you can call `assertNever` with its value to ensure the type remains `never`. When you add new values to the enum/union and you forget to handle it in the switch branches, the compiler will throw an error. For additional examples, see https://crrev.com/c/2589954

Let us know if you run into any issues.

Cheers,
LON

Connor Clark

unread,
Dec 14, 2020, 1:13:37 PM12/14/20
to Tim Van der Lippe, devtools-dev
I'm curious if an eslint rule would be better than code modifications required by 2. A quick google search shows this: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/switch-exhaustiveness-check.md 



--
You received this message because you are subscribed to the Google Groups "devtools-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to devtools-dev...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/devtools-dev/CAKCakL4gc%3DqwAkv%2Bg0u4GzYHYTU%3D%3D2uhKEz-QxN-3Of%2Bp1XY%2Bw%40mail.gmail.com.

Tim Van der Lippe

unread,
Dec 15, 2020, 5:48:32 AM12/15/20
to Connor Clark, devtools-dev
Ideally we would make use of such a rule. However, there are (a majority of) instances where our switch-statements are intentionally non-exhaustive (about 5 out of ~25 were intentionally exhaustive). By using `assertNever`, we provide the author the opportunity to decide what to do, while also making sure that on Runtime we have a descriptive error (if anything went wrong with version mismatches).
Reply all
Reply to author
Forward
0 new messages