Skip to content

In JS, class supports @template tag for declaring type parameters #23511

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

Merged
merged 8 commits into from
Apr 19, 2018

Conversation

sandersn
Copy link
Member

@sandersn sandersn commented Apr 18, 2018

Previously, classes did not understand the @template tag, so there was no way to declare and use type parameters on classes in Javascript.

This change makes the checker get the "effective" type parameters for a class, which means that Javascript classes also check their jsdoc for @template tags. The rest of the type parameter machinery is unchanged.

Edit: this PR now makes type parameters work with constructor functions as well. This required changes in name resolution and class instantiation since type parameters can be looked up outside their scope, and the anonymous type created by newing a Javascript constructor can now be instantiated.

Only the class support is needed to unblock webpack/webpack#7046 and webpack/webpack#7050, however.

Fixes #23459
Fixes #23384
Fixes #23385
Fixes #18135

Still need to do the following:
1. Correctly get jsdoc host in predicate.
2. Make this work for constructor functions too.
3. Scan rest of codebase for other usages of the type parameters
property that should be calls to getEffectiveTypeParameterDeclarations.
4. Rename tp to something more readable, like typar or ts'.
@sandersn sandersn requested a review from mhegazy April 18, 2018 17:21
@sandersn
Copy link
Member Author

CI failed because I missed a fourslash test. It's updated now.

@sandersn
Copy link
Member Author

I added support for @template in Javascript constructors as well. This required changes in name resolution and class instantiation since type parameters can be looked up outside their scope, and the anonymous type created by newing a Javascript constructor can now be instantiated.

@@ -2060,10 +2061,10 @@ namespace ts {
let symbol: Symbol;
if (name.kind === SyntaxKind.Identifier) {
const message = meaning === namespaceMeaning ? Diagnostics.Cannot_find_namespace_0 : Diagnostics.Cannot_find_name_0;

symbol = resolveName(location || name, name.escapedText, meaning, ignoreErrors ? undefined : message, name, /*isUse*/ true);
const secondarySymbol = resolveEntityNameFromJSPrototype(name, meaning);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider adding isInJavascriptFile just for clarity.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And maybe a comment that says "secondarySymbol is undefined except for type references in jsdoc on javascript prototype assignments"

Or something simpler than that. Hopefully.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants