Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to add generic function overload for module default export #31307

Open
saschanaz opened this issue May 8, 2019 · 2 comments
Open

Unable to add generic function overload for module default export #31307

saschanaz opened this issue May 8, 2019 · 2 comments
Labels
Bug A bug in TypeScript checkJs Relates to checking JavaScript using TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation Domain: Type Inference Related to type inference performed during signature resolution or `infer` type resolution
Milestone

Comments

@saschanaz
Copy link
Contributor

saschanaz commented May 8, 2019

TypeScript Version: 3.5.0-dev.20190508

Search Terms: module add overload

Code

// nanohtml.d.ts
export default function<T extends HTMLElement = HTMLElement>(strings: TemplateStringsArray, ...keys: any[]): T;
// @ts-check
// index.js
// install nanohtml@1.6.1
const nanohtml = require("nanohtml");

/** @type {HTMLAnchorElement} */
const a = nanohtml`<a href="example.com">Example</a>`;

Expected behavior:

a should be HTMLAnchorElement.

Actual behavior:

Error: Type 'HTMLElement' is missing the following properties from type 'HTMLAnchorElement': ...

@MartinJohns
Copy link
Contributor

MartinJohns commented May 8, 2019

You don't specify the type for T, and there is no argument that allows the compiler to infer the type from, so the compiler falls back to the default type for T which is HTMLElement.

If you specify the type then it works.

nanohtml<HTMLAnchorElement>`<a href="example.com">Example</a>`

Not a bug, works as intended.

@saschanaz
Copy link
Contributor Author

@MartinJohns Two things:

  1. The example is in JavaScript with checkJs. Sorry to provide no such indication.
  2. Even then, you don't have to explicitly give T. Try it yourself
declare function generic<T extends HTMLElement = HTMLElement>(num: number): T;

var x: HTMLAnchorElement = generic(1);

@weswigham weswigham added Bug A bug in TypeScript checkJs Relates to checking JavaScript using TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation Domain: Type Inference Related to type inference performed during signature resolution or `infer` type resolution labels May 8, 2019
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jun 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript checkJs Relates to checking JavaScript using TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation Domain: Type Inference Related to type inference performed during signature resolution or `infer` type resolution
Projects
None yet
Development

No branches or pull requests

4 participants