Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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.
- Loading branch information