gmod-typescript icon indicating copy to clipboard operation
gmod-typescript copied to clipboard

Replacing generated JS functions with builtin gmod functions

Open mattkrins opened this issue 2 years ago • 2 comments

Gmod comes with a number of builtin libraries such as the string library. For example:

// JavaScript version
print("haystack".endsWith("needle"))
// Gmod version
print(string.EndsWith("haystack","needle"))

Both of these functions will accomplish the same goal, however if using the JS version, the transpiler will need to add

local function __TS__StringEndsWith(self, searchString, endPosition)
    if endPosition == nil or endPosition > #self then
        endPosition = #self
    end
    return string.sub(self, endPosition - #searchString + 1, endPosition) == searchString
end

to the transpiled output. This will result in a lot of redundant code. Is there a way we can make TypeScriptToLua use the built-in gmod libraries? I'd be happy to write the logic to do so, just not sure if it is possible or where to start.

mattkrins avatar Oct 07 '23 08:10 mattkrins

Can't you just use the second version? string.EndsWith

lolleko avatar Oct 08 '23 20:10 lolleko

I suppose, but it means having to look at docs to use basic function prototypes people will generally already know in JS. It would be nice to have a TS compiler factory (or some such) to modify/override the functions being generated.

mattkrins avatar Oct 08 '23 21:10 mattkrins