Attempting to quiet TS2339 through the utilization of the suppressExcessPropertyErrors compiler setting

Take a look at this code snippet:

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

An issue arises with the TypeScript compiler showing the error message:

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

To tackle this warning for object literals, I assumed that adding suppressExcessPropertyErrors to the tsconfig.json file would resolve it.

Here's how I configured my tsconfig.json:

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

However, despite trying this solution, the compiler continues to display the same error message.

If you have any suggestions or tips, please share them! Thanks in advance :)

Answer №1

To resolve this issue, it seems that adding suppressExcessPropertyErrors to the tsconfig.json file could be a solution.

However, this only suppresses excess properties in object constructions like:

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

If you specifically want to ignore this warning for object literals,

You have the option to use the any type for flexibility, such as:

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

Learn More

This practice is known as lazy object initialization, and you can explore different approaches to handle it here:

Answer №2

While I do agree with some of the points made by @basarat, I would like to also offer my own suggestions.

There are a few reasons why declaring object properties like this could be beneficial:

var x = {
  test: ''
};
x.test = 'abc';
  1. Intellisense support is enabled, allowing you to easily see the available properties of the object when accessing it in the future.
  2. This approach is helpful for testing purposes. It ensures that the structure of your object remains consistent throughout your project, making it easier to write tests with this in mind.
  3. By avoiding using the `any` type and explicitly declaring object properties, you can prevent unintended overrides of existing properties with new ones.

An Explanation:

// original object x with originProp property
var x: any = {
  originProp: 'abc'
};
// creating a new object with a newProp property
x = {
  newProp:'abc'
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What data type should be used for process.stdin in a TypeScript/Node.js environment?

When faced with a variable that can either be fs.createReadStream('file-path') or process.stdin, the decision on which type to use can be difficult. It is important to determine if both returns are of type net.Socket, fs.ReadStream, or stream.Red ...

Avoid automatically opening mat-sidenav upon page load

Utilizing a BehaviorSubject in my service for mat-sidenav toggle has proven to be effective. toggle-service.ts export class ToggleSidenavService { public sideNavToggleSubject: BehaviorSubject<boolean> = new BehaviorSubject<boolean> ...

Is there a way to eliminate text from a barcode image using JavaScript or TypeScript?

This is my unique html code <div class="pr-2" style="width: 130px"> <div *ngIf="!element.editing" > <span class="ss">{{element.barcode}}</span> </di ...

Angular 16 impacts, notifications, processes

As a junior developer, I am currently immersing myself in the new functionalities of Angular 16. The latest tools like effects, signals, and computes are known for their power, but I am still trying to grasp their inner workings. My goal is to perform an A ...

View unprocessed HTML content - Angular 6

I want to showcase the raw HTML code (example.component.html) below 'example works!'. The page should display as follows: example works! <p> example works! </p> While there are resources available on how to do this with Ang ...

The Ionic search bar will only initiate a search once the keyboard is no longer in view

In my Ionic application, I have implemented a search bar to filter and search through a list. The filtering process is triggered as soon as I start typing in the search bar. However, the updated results are not displayed on the screen until I manually hide ...

Angular to always show loading spinner on page load

Utilizing my Angular project with the Ant Design NG Zorro loading spin. I encountered an issue where the loading spin is continuously active on my Angular page. Does anyone know the correct way to implement this loading spinner? Thanks View the example o ...

Is it possible for node modules to access and utilize protractor parameters?

Is it feasible for a node module to access a protractor parameter? I am in need of defining a parameter in my protractor conf.js file and then running a specific section of the shared node module js file across 5 projects. For instance, in my conf.js: ...

When the required attribute in Angular2 is conditional and the value is empty, the field will be adorned with the ng-valid class

In my current project using Angular 2.3.1, I have incorporated the following HTML element into a component template: <input class="form-control" type="text" id="password" name="password" placeholder="Password" [attr.required]="requirePasswd ? true : n ...

Maintain the selected list item active using ngFor after it is clicked

I am currently working with Angular and Typescript, and I have the following HTML code snippet. However, I am facing an issue where the items are not staying active after being clicked. <li *ngFor="let permission of tempPermission" class ...

Broken Encoding Issue with Server-Side HttpClient Response in Angular 5 Universal

I have encountered an issue with Angular 5 Universal and server side rendering (ssr). When I make a GET request using HttpClient on the server side, the response seems to have encoding problems. However, the same code works perfectly fine on the client sid ...

What steps can I take to turn a decorated service into an injectable component?

I created a decorator factory function that looks like this: export function CustomDecorator (dummyProp: string) { return function<T extends {new (...args: any[]): any}> (ctor: T) { @Injectable() class MyCustomClass extends ...

Instructions on utilizing *ngFor for every category I have

Seeking assistance with a specific issue. I currently have four labeled tabs, each containing projects. My goal is to ensure that when I save a project, it remains in the same tab where I initiated the save operation, rather than appearing across all tabs. ...

JavaScript - Persistent memory retention issues

I've noticed persistent memory leaks in my TypeScript application (3PG), leading me to believe there's an issue with memory management. Comparison of Applications: 2PG -> https://github.com/theADAMJR/2pg [no memory leaks] 3PG -> the speci ...

Issues with expanding all nodes in the Angular Treeview function by Nick Perkins in London are causing difficulties

Currently utilizing the angular treeview project found here: https://github.com/nickperkinslondon/angular-bootstrap-nav-tree After examining the functionality, it seems that this treeview is lacking search capabilities. To address this limitation, I deci ...

Tips for incorporating the ternary operator in JSX of a React component while utilizing TypeScript?

I am looking to implement a conditional rendering logic in React and TypeScript, where I need to return null if a specific condition is met, otherwise render a component using a ternary operator. Here is the code snippet I currently have: {condition1 && ...

How come I'm able to access the form's control within setTimeout but not outside of it?

Having just started working with Angular, I came across a strange issue involving forms and setTimeout. When trying to access the form control of an input element inside setTimeout within the OnInit lifecycle hook, it works fine. However, when attempting t ...

Setting up ESLint for TypeScript to enforce the "no-unused-vars" rule

In all my TypeScript projects, I utilize ESLint with specific settings in place: "extends": ["airbnb", "prettier", 'plugin:vue/recommended'], "plugins": ["prettier"], "parserOptions&quo ...

Revise regular expression for verifying addresses

In my angular app, I have created a regular expression for validating postal addresses. const regx = '\\b([p]*(ost)*\\.*\\s*[o|0]*(ffice)*\\.*\\s*b[o|0]x)\\b' I specifically want this ...

Setting setSelected to true in multiple ag-grids seems to only work for the final grid

Currently, I am using a for loop to iterate over an array and dynamically creating ag-grid based on its content. With the data available, I am preselecting certain rows in the ag-grid. For the gridReady method: onGridReady(event) { this.resultsArray ...