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

[BUGFIX] improve component argument types for TS-JS interop #286

Closed

Commits on Apr 24, 2020

  1. [BUGFIX] improve component argument types for TS-JS interop

    `Component` currently types its arguments as extending `{}`:
    
        class Component<Args extends {} = {}> {
          readonly args: Args;
          constructor(owner: unknown, args: Args);
        }
    
    This works in pure-JS or pure-TS codebases, but breaks down in mixed
    codebases, especially where users are using the `@ts-check` pragma or
    where we want to feed information from component definitions into the TS
    language server to get better completion info throughout an app.
    
    For example, given this component in a JS codebase with `@ts-check`
    enabled:
    
        class Profile extends Component {
          get description() {
            return `${this.args.name} is ${this.args.age} years old!`;
          }
        }
    
    The TS language server (e.g. in an editor) will report:
    
    > Property 'name' does not exist on type '{}'.
    > Property 'age' does not exist on type '{}'.
    
    To eliminate this problem while maintaining compatibility with all
    existing typed usages (including some we might like to forbid but cannot
    at present, like passing `Component<[]>` -- thanks JavaScript!), we
    switch from `{}` to `Record<string, any>`:
    
        class Component<Args extends Record<string, any> = Record<string, any>> {
          readonly args: Args;
          constructor(owner: unknown, args: Args);
        }
    
    Now, the error described above is eliminated for JS consumers using TS
    information in any way, but TS users can continue to supply correct
    types for their components.
    chriskrycho committed Apr 24, 2020
    Configuration menu
    Copy the full SHA
    9923764 View commit details
    Browse the repository at this point in the history