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

Type Constants #2

Open
thejustinwalsh opened this issue Oct 29, 2020 · 0 comments
Open

Type Constants #2

thejustinwalsh opened this issue Oct 29, 2020 · 0 comments
Labels
enhancement New feature or request

Comments

@thejustinwalsh
Copy link
Member

Currently constants are marked as any and declared with let and after browsing the source they appear to be of either number or string mostly, as well as indeed const. The docs themselves do not directly inform if they are a number or a string constant, however it is not entirely important to type as they are exposed to lua from the C++ API.

In order to provide some additional TypeScript type safeness we could brand each constant per module so you do not use the wrong constant in the wrong namespace...

declare enum DefoldConstant { _ = "" }
declare let GENERIC_CONSTANT: DefoldConstant;

We could parse the docs for the use of the constant, and read the type from the call site, as so far it appears the constants at the call site are referred to as either a number i,e resource.set_texture(path,table,buffer), a constant i.e resource.store_manifest() a constant | vector i.e. go.animate().

declare enum DefoldResourceConstant { _ = "" }
declare type ResourceTextureConstant = DefoldResourceConstant | number & {
}

declare type ResourceLiveUpdateConstant = DefoldResourceConstant & {
}

In the case of go.animate() the types are not directly called out and we would need to rely on some pattern matching. The parameter is named easing it is a constant | vector and we have constants that start with the name EASING_...

declare enum DefoldGoConstant { _ = "" }
declare type GoEasingConstant = DefoldGoConstant | vmath.vector3 & {
}

export const EASING_INBACK: GoEasingConstant 

The parser may need to be extended to store some additional context so that the generator can run some generation against the function calls in the module to determine naming convention and type.

Alternatively, constants could just be declared as constants scoped to the namespace, and the callsite itself go be modified when a number, vector, or some other constant type is presented to take either a scoped constant like ResourceConstant | type.

@thejustinwalsh thejustinwalsh added the enhancement New feature or request label Oct 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant