Tencent / puerts

PUER(普洱) Typescript. Let's write your game in UE or Unity with TypeScript.
Other
5.04k stars 702 forks source link

[Unity] Bug: Number overloads #1871

Open raad-genies opened 2 days ago

raad-genies commented 2 days ago

前置阅读 | Pre-reading

Puer的版本 | Puer Version

Latest

Unity的版本 | Unity Version

2022.3

发生在哪个平台 | Platform

Editor(mac)

错误信息 | Error Message

Not an error but more of an issue with how number overloads work. In the case of Random.Range Puerts currently has no way of distinguishing between the float and the int overloads which results in float always passing since number can always be a float.

Also any tips around how to create the same overload in the TS side would be great. Branding can work but then its essentially a new type and can't really just say var x : float = 9 for example.

问题重现 | Bug reproduce

Bind Random.Range and try to call it with decimals or without.

chexiongsheng commented 2 days ago

Due to the differences in the richness of types between C# and TypeScript/JavaScript, multiple C# types may map to a single JavaScript type. A suggested solution is to encapsulate the C# API with different names to avoid overloading. For example, instead of having Foo(float), Foo(int), and Foo(short), you can encapsulate them as Foo_float(float), Foo_Int(int), and Foo_short(short).

raad-genies commented 1 day ago

@chexiongsheng Is it possible to actually use branded types in TS defined globally like

 declare type float = number & { [floatBrand]: void };
 declare type int = number & { [intBrand]: void };

And include the alias info in the js call info? This would allow us to at least do type checking without having to resort to weird overload naming specially if you have some APIs that are engine specific.

chexiongsheng commented 17 hours ago

What are the declarations of floatBrand and intBrand?

raad-genies commented 5 hours ago

Unique symbol