havlock
havlock

Reputation: 690

Typescript Property 'prepend' does not exist on type 'HTMLDivElement'

In a typescript method, the code

some_existing_div.prepend(some_new_div)

produces:

[ts] Property 'prepend' does not exist on type 'HTMLDivElement'.

except, of course, it does, per https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/prepend.

tsconfig has

"lib": [ "es7", "es2017", "dom", "dom.iterable" ],

and I presume I need to add something to it. But what?

More generally, next time I get a similar problem, how do I solve it without coming here? Is there some mapping of newish features to typescript libs? Or what?

Upvotes: 0

Views: 1903

Answers (1)

Niklas Higi
Niklas Higi

Reputation: 2309

and I presume I need to add something to it. But what?

No, you don't need to add anything. The dom lib is supposed to contain all types that are part of the official DOM specifications. There is however a delay for new types because the official dom lib is generated based on files generated by Microsoft Edge which doesn't always implement the new specs right away and because TypeScript doesn't release a new version every time there is an update to the lib.

Regarding your specific question, the dom lib now includes the ParentNode.prepend method (on the master branch of the TypeScript repository) but the updated lib was not yet released as part of a version.

More generally, next time I get a similar problem, how do I solve it without coming here?

  1. Try updating your typescript package to the latest version.
  2. Check whether the current version of the dom.generated.d.ts file (which you can also build yourself) contains the type you are looking for. If it does, you can expect the type to be released as part of one of the next few TypeScript releases.
  3. Wait. And if you really need the API, either create a temporary .d.ts file — this only works for completely new types — or cast the object you're working with to any. This would be (some_existing_div as any).prepend(some_new_div) in your case.

Upvotes: 2

Related Questions