teal-language / tl

The compiler for Teal, a typed dialect of Lua
MIT License
2.03k stars 101 forks source link

Allow literal types #645

Open TopchetoEU opened 1 year ago

TopchetoEU commented 1 year ago

As a user, coming from TypeScript, I find it extremely convenient to have literal types (for example, the type 'foo'). This allows for some much more advanced typing (in typescript), for example, events:

interface EventHandler {
    (target: 'foo', handle: (val: string) => void);
    (target: 'bar', handle: (val: number) => void);
    (target: 'baz', handle: (val: boolean) => void);
}

However, doing the same in lua is impossible, so we need to compromise type checking:

global type EventHandler = function(target: string, handle: function(val: any): nil)

I'm well aware that Typescript and Teal don't have the same design philosophies, but I would really appreciate this feature being "borrowed" from Typescript.