Comment

Comment by ghh

This is kind of a random Typescript tip, but when migrating regular Javascript, there is an alternative to adding the type 'any' to every object to 'silence' the compiler.

That is to introduce a preliminary type definition. Instead of:

  const oldVar: any = { field: 1, ... }
  function foo(bar: any) { ... }
You can write:

  declare type OldVarType = any

  const oldVar: OldVarType = { field: 1, ... }
  function foo(bar: OldVarType) { ... }
This way, you can signal that it's not just any kind of any, but a particular kind of any, which is now trackable in your codebase.

When you're ready, you can gradually update the OldVarType declaration and solve the compiler type check warnings from there. Union types [1] can be quite useful then too.

[1] https://www.typescriptlang.org/docs/handbook/advanced-types....

parent ghh 10 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton