Skip to content

GlobalEventHandlers.onerror should be of type ErrorEventHandler | null #29876

Closed
@thdk

Description

@thdk

TypeScript Version: 3.2.1

Search Terms: lib.dom.ts GlobalEventHandlers onerror

Code

const imageEl = document.createElement("img");

// enable strictNullChecks in options and below line will give a type error
// type 'null' is not assignable to type 'ErrorEventHandler'
imageEl.onerror = null;

Expected behavior:
it should be possible to assign null to onerror

Actual behavior:
type 'null' is not assignable to type 'ErrorEventHandler'

Playground Link:
Playground link

Related Issues:
#28638 (not really related)


I believe line https://github.com/Microsoft/TypeScript/blob/master/lib/lib.dom.d.ts#L5478 can be modified to allow null as follows:
Option 1:

/**
     * Fires when an error occurs during object loading.
     * @param ev The event.
     */
    onerror: ErrorEventHandler | null;

Option 2

interface ErrorEventHandlerNonNull {
    (event: Event | string, source?: string, fileno?: number, columnNumber?: number, error?: Error): void;
}

and a type ErrorEventHandler

type ErrorEventHandler = ErrorEventHandlerNonNull | null;

and leave 'onerror: ErrorEventHandler;' as is.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions