'Typescript object parameter to type

With this code foo.bar is of type any. How do I change it to type of number?

  const foo = {} as any

  foo.bar = 4 as number


Solution 1:[1]

You can define properties as optional if you are not sure which all properties will be there.

type Foo = {
  bar?: number
}

const foo: Foo = {}
foo.bar = 4

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dhaker