[Solved] typescript:error TS2531: Object is possibly ‘null’.

Why? Yes, the TS compiler diagnosed document at compile time Getelementbyid (‘test ‘) may be null, so a prompt is given: the object may be null. The solution is as follows:

document.getElementById('test')!.innerHTML = greeter(user);

Add one!, What does the exclamation point mean?It is actually a not null assertion operator. It does not perform runtime check and tells the compiler that it only needs to know this thing

Similar Posts: