Skip to content

Conversation

@sandersn
Copy link
Member

Previously, type references to generic types would fail to copy type parameters from type references in an @type tag. Now the type
parameter is copied.

Note that I left 3 places in the checker unchanged, even though they should technically also get type parameters from type references:

  1. getOuterTypeParameters -- not fixing this means, I think, that lookup of a type parameter still won't get instantiated correctly in the body of a function.
  2. getLocalTypeParameterOfClassOrInterfaceOrTypeAlias -- only applies to constructor functions which have a type annotation, which doesn't type check in the first place.
  3. isThislessType -- It isn't possible to refer to this correctly in a typedef, as far as I can tell.

I'm experimenting with (1) but this fix makes it usable from the call site at least, and fixes the original bug.

Fixes #49039

Previously, type references to generic types would fail to copy type
parameters from type references in an `@type` tag. Now the type
parameter is copied.

Note that I left 3 places in the checker unchanged, even though they
should technically also get type parameters from type refereneces:

1. getOuterTypeParameters -- not fixing this means that nested function
still won't get instantiated correctly. I'll see how hard this is to
fix.
2. getLocalTypeParameterOfClassOrInterfaceOrTypeAlias -- only applies to
constructor functions which have a type annotation, which doesn't type
check in the first place.
3. isThislessType -- This function is conservative, so should be
possible to make it more conservative without writing a lot of code.

Fixes #49039
@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Jul 19, 2022
@sandersn sandersn requested review from gabritto and weswigham July 19, 2022 21:21
@sandersn sandersn merged commit 7f3ca9f into main Jul 19, 2022
@sandersn sandersn deleted the copy-generics-to-function-with-type-tag branch July 19, 2022 22:40
@sandersn
Copy link
Member Author

Re (1): In Typescript, you can't refer to parameters of type T in the body of a function whose type is contextually typed by a type alias to a generic arrow type:

export type Id = <T>(m : T) => T;
const inTsArrow: Id = m => {
    const o: T = m
    return o;
}

Without that, there's no reason to try to collect outer type parameters in JS.

@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 22, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Author: Team For Milestone Bug PRs that fix a bug with a specific milestone

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Irregular behavior of generic non arrow function that is typed via JSDoc import.

4 participants