Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular error TS2531: Object is possibly 'null'

So I have a Component.html that includes an input as follows:

<input type="text" (change) = "setNewUserName($event.target.value)"/>

the component.ts is:

import { Component } from "@angular/core";
@Component({
    selector : 'app-users-list',
    templateUrl : './usersList.component.html'
})
export class UsersListComponent 
{
   setNewUserName (userName : string): void {
       console.log('setNewUserName', userName)
   }
}

and finally the module.ts is:

@NgModule ({
    declarations: [UsersListComponent],
    imports : [CommonModule],
    exports: [UsersListComponent]
})
export class UsersListModule {}

When running the server, the following error pops up:

error TS2531: Object is possibly 'null'.

1 <input type="text" (change) = "setNewUserName($event.target.value)"/>
                                                              ~~~~~
like image 688
Nour Mawla Avatar asked Apr 16 '21 10:04

Nour Mawla


People also ask

How do I fix TypeScript object is possibly null?

The "Object is possibly 'null'" error occurs when we try to access a property on an object that may have a value of null . To solve the error, use the optional chaining operator or a type guard to make sure the reference is not null before accessing properties.

How do I tell TypeScript to ignore undefined?

The exclamation mark is the non-null assertion operator in TypeScript. It removes null and undefined from a type without doing any explicit type checking.

Is of type unknown?

The "Object is of type unknown" error occurs when we try to access a property on a value that has a type of unknown . To solve the error, use a type guard to narrow down the type of the object before accessing a property, e.g. if (err instanceof Error) {} . Copied!


3 Answers

Are you using Angular Ivy? Most possibly it's due to the template type checking in Ivy AOT.

Nevertheless, there are multiple options

Option 1: Send the event as argument

<input type="text" (change) = "setNewUserName($event)"/>
export class UsersListComponent {
   setNewUserName (event: any): void {
       console.log('setNewUserName', event.target.value)
   }
}

Option 2: Use a template reference variable

<input #userName type="text" (change) = "setNewUserName(userName.value)"/>
export class UsersListComponent {
   setNewUserName (userName : string): void {
       console.log('setNewUserName', userName)
   }
}

Option 3: Disable type checking using $any()

<input type="text" (change) = "setNewUserName($any($event.target).value)"/>
export class UsersListComponent {
   setNewUserName (userName : string): void {
       console.log('setNewUserName', userName)
   }
}

Option 4: Template-driven or reactive form

Use a template-driven or reactive form to get the input value. IMO this would be the most elegant approach.

Update: add disable type checking

like image 172
ruth Avatar answered Oct 12 '22 14:10

ruth


try this

setNewUserName($any($event.target).value);

it will work

like image 27
Ankit Verma Avatar answered Oct 12 '22 15:10

Ankit Verma


this worked for me

($any($event.target).value);

in angylar 13

like image 1
mojgan Avatar answered Oct 12 '22 14:10

mojgan