Jump to content

Typescript typing issues


Kesshi
 Share

Recommended Posts

I just stumbled upon some typing issues.
1. The ratio parameter in the PostProcess contructor is defined as "number | any" but in all derived classes (FxaaPostProcess ...) it is define as "number" only. see:
https://github.com/BabylonJS/Babylon.js/blob/master/src/PostProcess/babylon.postProcess.ts#L100
https://github.com/BabylonJS/Babylon.js/blob/master/src/PostProcess/babylon.fxaaPostProcess.ts#L6

2. There are some issues with the size parameter of the RenderTargetTexture:
- in the constructor its defined as "any" but it should be "number | any"
- the private member "_size" is defined as "number" but should be "number | any"
- the getRenderSize() function returns "number" but should return "number | any"
- the scale() function expects "_size" to be "number" and will fail if it is an object:
https://github.com/BabylonJS/Babylon.js/blob/master/src/Materials/Textures/babylon.renderTargetTexture.ts#L168

Link to comment
Share on other sites

It wouble be better not to use "any" as the type for function parameters. Because if it is not documented, you need to look at the sources to see which Parameters you can use for any. For example for the PostProcess it would be better to define "ratio" as "number | {width: number, height: number}".

One more thing i don't like are things like samplingMode, coordinatesMode, fovMode .... They are always declared as number and its not obvious what the possible values are. Sometimes you can see it in the naming (e.g. Texture.NEAREST_SAMPLINGMODE) but in some cases its not so easy to see. For example the Camera.mode. Here you can use Camera.ORTHOGRAPHIC_CAMERA and Camera.PERSPECTIVE_CAMERA but its not clear.
I always define things like this as enums. This makes it absolutly clear what values you can use and you get an compiler error if you assign something wrong.

Link to comment
Share on other sites

3 hours ago, Deltakosh said:

Only problem of enum: they generate a string and this is not possible to have to test against a string while rendering a frame.

That is not true. This example will print "x=2 type=number":

enum Test {
    Value1,
    Value2,
    Value3
}

let x = Test.Value3;
alert("x=" + x + " type=" + typeof (x));

You can even specify the numeric value like this:

enum Test {
    Value1 = 12,
    Value2 = 13,
    Value3 = 14
}

Regarding the PR. I will put it on my TODO list. If i have some time at work i can work on it.
 

Link to comment
Share on other sites

Ok but you can declare the enum as "const" and set the "preserveConstEnums" compiler option to true. Now this:

const enum Test {
    Value1 = 12,
    Value2 = 13,
    Value3 = 14
}
var x = Test.Value1;
if (x === Test.Value2) {
}

 

compiles to this:

var Test;
(function (Test) {
    Test[Test["Value1"] = 12] = "Value1";
    Test[Test["Value2"] = 13] = "Value2";
    Test[Test["Value3"] = 14] = "Value3";
})(Test || (Test = {}));

var x = 12 /* Value1 */;
if (x === 13 /* Value2 */) {
}

 

If you do not set the " preserveConstEnums" you will only get:

var x = 12 /* Value1 */;
if (x === 13 /* Value2 */) {
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...