Skip to content

Use generic return types for frequently-casted library functions #18415

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

Closed
RyanCavanaugh opened this issue Sep 12, 2017 · 3 comments
Closed

Use generic return types for frequently-casted library functions #18415

RyanCavanaugh opened this issue Sep 12, 2017 · 3 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Suggestion An idea for TypeScript

Comments

@RyanCavanaugh
Copy link
Member

There are a handful of functions from lib.d.ts which are typically followed by a type assertion

const x = <HTMLDivElement>document.getElementById('zzz');

With the new inference changes, we can write a new overload

getElementById<T extends Element>(id: string): T

so that you can write the cleaner version

const x: HTMLDivElement = document.getElementById('zzz');

TODO: Figure out which functions these apply to

@RyanCavanaugh RyanCavanaugh added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Sep 12, 2017
@mhegazy mhegazy added the Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript label Sep 12, 2017
@gcnew
Copy link
Contributor

gcnew commented Sep 13, 2017

Such generics will give a false sense of security. IMO, casts are what's appropriate there as they convey the "I know better" assertion.

@zspitz
Copy link
Contributor

zspitz commented Sep 13, 2017

The following from the DefinitelyTyped readme is relevant:

Common mistakes

...

  • getMeAT<T>(): T:
    If a type parameter does not appear in the types of any parameters, you don't really have a generic function, you just have a disguised type assertion.
    Prefer to use a real type assertion, e.g. getMeAT() as number.
    Example where a type parameter is acceptable: function id<T>(value: T): T;.
    Example where it is not acceptable: function parseJson<T>(json: string): T;.
    Exception: new Map<string, number>() is OK.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 7, 2017

Though convenient, it is clearly unsafe. users who would want to do that can do it themselves as overloads.

@mhegazy mhegazy added Declined The issue was declined as something which matches the TypeScript vision and removed In Discussion Not yet reached consensus labels Nov 7, 2017
@mhegazy mhegazy closed this as completed Nov 7, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Declined The issue was declined as something which matches the TypeScript vision Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants