Skip to content
This repository has been archived by the owner on Jun 1, 2022. It is now read-only.

Improve Component types to allow for type inference on schema properties #263

Closed
wants to merge 2 commits into from
Closed

Improve Component types to allow for type inference on schema properties #263

wants to merge 2 commits into from

Conversation

brmenchl
Copy link

@brmenchl brmenchl commented Oct 1, 2020

These changes should enable type inference on component schema properties, while not changing the way Components are constructed.

Now when using Typescript and creating a component like: class Position extends Component<{ x: number, y: number }>{} for any new instance of Position typescript will recognize all base Component properties, as well as the properties specified in the Component generic type parameter (the schema). (in this case, new Position().x will resolve to number)

Example usage (type declarations copied from PR)

Fixes #249

@ulyssesdotcodes
Copy link

Does this work with Entity.d.ts? I think taking out the generic from ComponentConstructor makes it more difficult to getComponent

@brmenchl
Copy link
Author

brmenchl commented Oct 5, 2020

@ulyssesp Good catch. I modified component types to allow parametrizing component constructors. For now, there are new two type entry points. One, where you know the schema type already (referencing existing Component constructors, you can now reference ComponentConstructor), and two, where you don't (now this is just called ComponentStatic, and only the constructor function new is parametrized).

@brmenchl
Copy link
Author

brmenchl commented Oct 5, 2020

I also added simple type tests to the repo to show usage, and updated all type files to reference the correct types

@brmenchl
Copy link
Author

brmenchl commented Oct 5, 2020

Typescript playground link with updated PR types here

@DavidPeicho
Copy link
Contributor

Pretty cool. The only thing is that at the end we duplicate the type information in the extends and schema. I am wondering if it would be possible to directly use the schema.

@brmenchl
Copy link
Author

brmenchl commented Oct 7, 2020

In Typescript, The static members of a generic class can never refer to the class's type parameters, so we can't reference back and forth from scheme to the type parameters, we have to duplicate.

https://microsoft.github.io/TypeScript-New-Handbook/chapters/classes/#type-parameters-in-static-members

@brmenchl
Copy link
Author

@robertlong (or some other maintainer), are we good to merge this?

@brmenchl brmenchl closed this by deleting the head repository May 28, 2024
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.

Improvement: Inferring schema types on Components
3 participants