Skip to content

Commit a5002a3

Browse files
authored
Luau Type Improvements (#76)
This PR makes some improvements to the existing Luau types, including: Improves reuse of the Action type Fixes contravariance issue with Store type Allows thunks to be void returning
1 parent 8c8da37 commit a5002a3

File tree

8 files changed

+27
-16
lines changed

8 files changed

+27
-16
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ roblox.toml
1717
# Darklua
1818
**/darklua*
1919

20+
# Analysis artifacts
21+
sourcemap.json
22+
globalTypes.d.lua
23+
2024
# Misc OS and editor files
2125
.DS_Store
2226
.vscode

foreman.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tools]
2-
rojo = { source = "rojo-rbx/rojo", version = "7.2.0" }
3-
selene = { source = "Kampfkarren/selene", version = "0.21.1" }
4-
stylua = { source = "JohnnyMorganz/StyLua", version = "0.15.1" }
5-
luau-lsp = { source = "JohnnyMorganz/luau-lsp", version = "*" }
6-
wally = { source = "UpliftGames/wally", version = "0.3.1" }
2+
rojo = { source = "rojo-rbx/rojo", version = "=7.2.1" }
3+
selene = { source = "Kampfkarren/selene", version = "=0.21.1" }
4+
stylua = { source = "JohnnyMorganz/StyLua", version = "=0.15.1" }
5+
luau-lsp = { source = "JohnnyMorganz/luau-lsp", version = "=1.8.1" }
6+
wally = { source = "UpliftGames/wally", version = "=0.3.1" }

src/loggerMiddleware.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
--!strict
21
-- We want to be able to override outputFunction in tests, so the shape of this
32
-- module is kind of unconventional.
43
--

src/makeActionCreator.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@
55

66
local actions = require(script.Parent.types.actions)
77

8+
export type Action<Type> = actions.Action<Type>
9+
810
export type ActionCreator<Type, Action, Args...> = actions.ActionCreator<Type, Action, Args...>
911

10-
local function makeActionCreator<Type, Action, Args...>(name: Type, fn: (Args...) -> Action): ActionCreator<Type, Action, Args...>
12+
local function makeActionCreator<Type, Payload, Args...>(name: Type, fn: (Args...) -> Payload): ActionCreator<Type, Payload, Args...>
1113
assert(type(name) == "string", "Bad argument #1: Expected a string name for the action creator")
1214

1315
assert(type(fn) == "function", "Bad argument #2: Expected a function that creates action objects")
1416

1517
return setmetatable({
1618
name = name,
1719
}, {
18-
__call = function(_self: any, ...: Args...): Action & { type: Type }
20+
__call = function(_self: any, ...: Args...): Payload & Action<Type>
1921
local result = fn(...)
2022

2123
assert(type(result) == "table", "Invalid action: An action creator must return a table")

src/makeThunkMiddleware.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
--!strict
21
--[[
32
A middleware that allows for functions to be dispatched with an extra
43
argument for convenience. Functions will receive two arguments:

src/types/actions.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ export type Action<Type = any> = {
33
type: Type,
44
}
55

6-
export type AnyAction = Action & {
6+
export type AnyAction = {
77
[string]: any,
8-
}
8+
} & Action
99

10-
export type ActionCreator<Type, Action, Args...> = typeof(setmetatable(
10+
export type ActionCreator<Type, Payload, Args...> = typeof(setmetatable(
1111
{} :: { name: Type },
12-
{} :: { __call: (any, Args...) -> (Action & { type: Type }) }
12+
{} :: { __call: (any, Args...) -> (Payload & Action<Type>) }
1313
))
1414

1515
return nil

src/types/store.lua

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@ export type Dispatch<State = any> = IDispatch<Store<State>>
1313
export type IStore<State, Dispatch> = {
1414
dispatch: Dispatch,
1515
getState: (self: IStore<State, Dispatch>) -> State,
16-
destruct: (self: IStore<State, Dispatch>) -> (),
17-
flush: (self: IStore<State, Dispatch>) -> (),
16+
17+
--[[
18+
FIXME LUAU: Typing self as any here is a hack to skirt around
19+
variance-related issues with tables. Read-write properties
20+
should obviate the need for this workaround.
21+
]]
22+
destruct: (self: any) -> (),
23+
flush: (self: any) -> (),
24+
1825
changed: RBXScriptSignal,
1926
}
2027
export type Store<State = any> = IStore<State, Dispatch<State>>

src/types/thunks.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local store = require(script.Parent.store)
44
type IStore<State, Dispatch> = store.IStore<State, Dispatch>
55
type IDispatch<Store> = store.IDispatch<Store>
66

7-
export type IThunkAction<ReturnType, Store> = (store: Store) -> ReturnType
7+
export type IThunkAction<ReturnType, Store> = ((store: Store) -> ReturnType) | ((store: Store) -> ())
88
export type ThunkAction<ReturnType, State = any> = IThunkAction<ReturnType, ThunkfulStore<State>>
99

1010
export type IThunkDispatch<Store> = <ReturnType>(

0 commit comments

Comments
 (0)