String.gsub to remove type annotations

I need a gsub function that removes type annotations from a string
e.g
local a: string = "hello"
into
local a = "hello"

I did this with string.gsub(str, ":(.*)=", " =") but I also need it for functions
E.g.
local function testFunction(varA: string|boolean, varB): CFrame
into
local function testFunction(varA, varB)

a general solution is vastly difficult as the syntax of the types can be complex.
For simple cases like your example, the following will work:

string.gsub(str, ": *[%w|]+", "")

it is assumed that there are no spaces between tokens.

2 Likes

Amazing! Thank you so much :smile:

I’ve also modified it to include type annotations such as

local a = "foo" :: string

and

local a: {string: number} = {["Test"] = 5}

by using this pattern:

str:gsub("::", ":"):gsub(": *[%w|%{%}]+", "")

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.