fewkz / froact

Wrapper around Roact & Roact Hooks to simplify UI development and add full, correct Luau typing
MIT License
3 stars 0 forks source link

Add ref types #12

Open fewkz opened 1 year ago

fewkz commented 1 year ago

Make a froact.createRef() function that returns { current: Instance } with the code:

type Ref = { current: Instance }
local function createRef(): Ref
    return froact.Roact.createRef()
end

As it turns out, you can do .current on Roact refs. We can type the ref property on Roblox instance components as { current: Instance } so that you'll never fuck up the type of refs ever!

We get the beautiful, absolutely amazing extravagance of branch-based luau type inference! This is absolutely invaluable. image

oh, and while we're add it, add froact.useRef(hooks), instead of doing hooks.useMemo(froact.createRef(), {})

fewkz commented 1 year ago

not sure about this, actually, refs being typed as any is nice, because you can usually assume the value of a ref correctly, there's mostly not going to be a case where you really want to assert whatever it's value is. Getting autocomplete for .current is nice, but wow is it nit-picking.

fewkz commented 1 year ago

An implementation of this is available in the https://github.com/fewkz/froact/tree/ref-binding branch