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

Improved TypeScript ergonomics #12

Closed
bennypowers opened this issue Jun 7, 2021 · 6 comments
Closed

Improved TypeScript ergonomics #12

bennypowers opened this issue Jun 7, 2021 · 6 comments

Comments

@bennypowers
Copy link
Member

bennypowers commented Jun 7, 2021

This issue tracks some nice-to-have enhancements for type detection.

The analyzer does great picking up on explicitly typed members:

function XMixin<B extends Constructor<{ b: B }>(superclass: B) {
  return class XL<A> extends superclass {
    x: X<A> = new X<A>(this.b);
    bool: boolean = true;
  }
}

Here,

  • the mixin parameter will be correctly typed as B
  • the x field will be correctly typed as X<A>
  • the bool field will be typed as boolean

However, it would be nice if for the following code:

function XMixin<B extends Constructor>(superclass: B) {
  return class XL<A> extends superclass {
    x = new X<A>();
    bool = true;
  }
}

Would give:

  • the parameter as B extends Constructor<{ b: B }>
  • the x field as X<A> (even without an explicit typing)
  • the bool field as boolean (even without an explicit typing)

I'm guessing that the TypeScript API has the chops to infer those things, given a SourceFile AST, but I haven't looked into it just yet.

@bennypowers
Copy link
Member Author

bennypowers commented Jun 7, 2021

I've got a plugin for boolean types, want that in core @thepassle ?

@thepassle
Copy link
Member

What does the plugin do exactly? Assign the type of bool to be {type: {text: boolean}} by inferring the type from its default value?

I think we could add some type inference behavior for fields, (and variables?), but we'd need to make sure to keep the correct 'priority' of type systems. Currently JSDoc will overrule TS types, so the priority should probably be (from least prio to highest prio):

  • inferred type
  • TS type (overrules inferred)
  • JSDoc type (overrules TS)

@bennypowers
Copy link
Member Author

bennypowers commented Jun 8, 2021

Since op i updated the plugin to handle

  • boolean initializers
  • number initializers
  • string initializers
  • T as const initializers
        if (isBoolean(node))
          memberDoc.type = { text: 'boolean' };
        else if (isAsConst(node.initializer, api))
          memberDoc.type = { text: node.initializer.expression.getText() };
        else if (api.isStringLiteral(node.initializer))
          memberDoc.type = { text: 'string' };
        else if (api.isNumericLiteral(node.initializer))
          memberDoc.type = { text: 'number' };

@bennypowers
Copy link
Member Author

I'll open a draft PR and we can go over it there 🙂

@thepassle
Copy link
Member

I think we should make a new handler for this, something like handleTypeInference and add it here: https://github.com/open-wc/custom-elements-manifest/blob/master/packages/analyzer/src/features/analyse-phase/creators/createClassField.js#L18

fieldTemplate = handleTypeInference(fieldDoc, node);

That way we can easily add it for variables as well

@thepassle
Copy link
Member

type inference was added, ill close this for now

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

No branches or pull requests

2 participants