Search code examples
angulartypescriptionic2

Trying to suppress TS2339 by suppressExcessPropertyErrors compiler option


Here is the code:

var x = {};
x.test = 'abc';

Getting typescript compiler error:

TS2339: Property 'test' does not exist on type '{}'.

I want to suppress this warning for object literals, I suppose that putting suppressExcessPropertyErrors into tsconfig.json should solve this.

tsconfig:

{
    "compilerOptions": {
        "suppressExcessPropertyErrors": true
    },
    ...
}

But nothing changed.. compiler still showing the error.

Thank you for any hints ;)


Solution

  • I suppose that putting suppressExcessPropertyErrors into tsconfig.json should solve this.

    No. It suppresses excess properties in object construction e.g.

    var x = {};
    x = {test:'abc'};
    

    I want to suppress this warning for object literals

    You can do anything you want using the any type e.g.

    var x:any = {};
    x.test = 'abc';
    

    More

    This is called lazy object initiliazation and patterns for dealing with it are covered here : https://basarat.gitbook.io/typescript/main-1/lazyobjectliteralinitialization