As soon as you move the code to TypeScript you will start to get Errors like the following:

    1. foo.bar = 123; // Error: Property 'bar' does not exist on type '{}'
    2. foo.bas = "Hello World"; // Error: Property 'bas' does not exist on type '{}'

    This is because from the state let foo = {}, TypeScript infers the type of foo (left hand side of initializing assignment) to be the type of the right hand side {} (i.e. an object with no properties). So, it error if you try to assign to a property it doesn’t know about.

    This is also great for code review and code maintainability purposes.

    If you have a large JavaScript code base that you are migrating to TypeScript the ideal fix might not be a viable solution for you. In that case you can carefully use a type assertion to silence the compiler:

    1. let foo = {} as any;
    2. foo.bas = "Hello World";
    • Good Docs
    • Safe assignment

    This is shown below:

    Here is a quick example that shows the fact that using the interface can save you:

    1. interface Foo {
    2. bar: number
    3. bas: string
    4. let foo = {} as Foo;
    5. foo.bar = 123;
    6. foo.bas = "Hello World";
    7. // later in the codebase:
    8. }