-
Notifications
You must be signed in to change notification settings - Fork 214
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
feat: .d.ts type annotations #177
feat: .d.ts type annotations #177
Conversation
Is this based on ogl-types? I was also working on this and have more type definitions to add... |
Yes, I can defer changes here, this is at least complete for the core/math methods. These annotations were generated via |
Cool, no go for it, this is great. 👍 I found the same and also have some of the extras ready to go. I've been testing them so far with a vanilla TS and Rollup build and the examples as TS. I'll test this PR against what I have for both a vanilla TS project and Angular, I'm expecting they'll be the same but will let you know if I find any differences, cheers! 😉 |
Alright, I've created a CodeSandbox template of my vanilla TS setup we can use for testing if you like. In the // package.json
{
"dependencies": {
"ogl": "CodyJasonBennett/ogl#feat/type-annotations"
}
} For example, here's the first OGL example from the readme as a really quick TS file, it's not fully typed but works as a quick way to validate any issues with the type definitions. In CodeSandbox it takes a minute for the definitions to load in the web interface, but they do work there. So in this first example the only issue is the missing
https://codesandbox.io/s/ogl-types-readme1-9qhjff And in my Angular project there were a number of issues that needed to be fixed with ogl-types, one of which you had already fixed above for the Here's the other ones:
As a quick hack I did the following down all the connected lookAt(target: Vec3 | [number, number, number]): this
export type Attribute = {
size: number
data: Float32Array fromArray(a: Float32Array | Array<number>, o?: number): this
toArray(a?: Float32Array | Array<number>, o?: number): Float32Array | Array<number> I have some more fixes too but for the extras. All this to say, even though we could merge this PR as it's only for core/math, we shouldn't do a new release for these typings until they are feature complete and at the very least work with all the OGL examples imho. Perhaps we merge this PR for now, and then follow-up with subsequent PRs for fixes and the remaining extras? I'll also create tests for the above issues where we can validate the fixes. Let me know what you guys think! 😊 |
There are tuple types for each math class and their array form. They represent cases where only the data is expected, not the math interface. All math methods could expect this tuple type since the gl-matrix functions only operate on arrays. If that is only supposed to be an implementation detail, perhaps Geometry indeed can only accept float typed arrays for attributes ( I'm not inclined to merge anything until it's ready to release. I'll leave that criteria to you although this PR was only intended as a closer baseline than ogl-types. Perhaps we can work off of a |
Sounds good, and ya it'll be awhile before the types are ready for a release so I'm in support of a |
I tried export interface Attribute {
size: number;
data: ArrayBufferView; And get the following error:
Unless you know of another way I think we'll need to specify all three, which works: export interface Attribute {
size: number;
data: Float32Array | Uint32Array | Uint16Array; |
Thanks, latest commit works great! 👍 Have another question/clarification on the use of the tuple types, for example It's not used in the set(x: Vec3 | number[] | number, y?: number, z?: number): this; |
See my comment in #177 (comment). Again, an array of any length has a different interface than a class which extends an array and implements class methods. That's why |
Not sure I follow, are you saying to do something like the following with multiple signatures? set(x: number, y?: number, z?: number): this;
set(v: Vec3): this;
set(a: Vec3Tuple): this; |
It needs to be known whether it's valid to pass an array over a math class anywhere. I'm personally not a fan of leaking At the very least, overloads' last signature should combine all the others so that dynamic inference in TS will work. set(x: number, y?: number, z?: number): this;
set(v: Vec3): this;
set(a: Vec3Tuple): this;
// Needed since TS doesn't join overloads for you.
// https://github.com/microsoft/TypeScript/issues/32164
set(x: number | Vec3 | Vec3Tuple, y?: number, z?: number): this; |
Got it, thanks for the explanation! Let's go with the combined signature then in the meantime. 😊 |
Alright, so I finally have a cleaned-up and nearly complete set of types for both the core/math classes and the extras that works with all 46 examples (48 if you include the readme examples). Related PR: You can see the updated types on my fork: @CodyJasonBennett would you like me to update this existing PR, or open a new one? |
Also for whatever reason the CodeSandbox definitions don't load anymore. 🤷♂️ It's working for me locally though if you install directly from my fork, for example: npm i pschroen/ogl#feat/types or yarn add ogl@pschroen/ogl#feat/types |
I'm not able to champion this at the moment so please do open a new PR. Codesandbox I've noticed having issues with types resolving everywhere, not just here. |
Continuing in #188. |
Partial implementation of #24. Adds type annotations for core/math. I won't attempt extras for now.
Validated against TS source -- https://github.com/CodyJasonBennett/ogl/tree/feat/types.