Encountering an issue when attempting to declare an overloading function type with a full type signature in TypeScript.
For example:
// Full type signatures for functions
type CreateElement = {
(tag : 'a') : HTMLAnchorElement,
(tag : 'canvas') : HTMLCanvasElement,
(tag : 'table') : HTMLTableElement,
(tag:string) : HTMLElement
}
// Implementing the functions
let createElement:CreateElement = (tag:string):HTMLElement => {
return document.createElement(tag)
}
/* Error message:
Type '(tag: string) => HTMLElement' is not assignable to type 'CreateElement'.
Type 'HTMLElement' is missing properties such as charset, coords, download, hreflang, etc.
*/
However, this alternative approach works fine:
function createElement(tag:'a'):HTMLAnchorElement
function createElement(tag:'canvas'):HTMLCanvasElement
function createElement(tag:'table'):HTMLTableElement
function createElement(tag:string):HTMLElement
function createElement(tag:string) {
return document.createElement(tag)
}