Comments (7)
Made a PR for this: #84
from roblox-lua-promise.
It feels like Luau doesn't have the features we need to type Promises correctly yet.
You can either infer types from the constructor, or declare the types in a typedef and have them be completely unchecked. (You also won't get the metatable as part of the type if you do the typedef route as well)
The inferred type of the constructor will contain private fields, and doesn't seem like it can support generics. We ideally want the Promise type to be Promise<T..., E...>
You'd start it off by saying export type Promise<T> = typeof(Promise.new(function end))
... and then there's nowhere to put the type variable, because Luau doesn't support passing generic types explicitly
Both of these options seem like non-starters to me. Having private fields and no types for the inner resolved/rejected values in the Promise type because we can't specify generic arguments is not useful.
On the other hand, using a large type definition is separated from the code and is completely unchecked.
Here's a small example of that:
type Promise = {
andThen: (Promise, (...any) -> Promise | any) -> Promise
}
local Promise = {}
Promise.__index = Promise
-- Type Error: (11,2) Type '{ @metatable Promise, { } }' could not be converted into 'Promise'
function Promise.new(): Promise
return setmetatable({}, Promise)
end
function Promise:andThen(func: (...any) -> Promise | any): Promise
end
This doesn't type check. The only option is to cast the return value of Promise.new
into the Promise
type manually, which is not sound.
I don't think Luau type checking quite has the tools required to type the Promise library yet.
from roblox-lua-promise.
I completely agree that hacky solutions are no good here. I don't know if these issues are currently being addressed by the Luau team or not- but it wouldn't hurt to let them know. I don't develop anymore on the platform, but Promises were a really big part of the codebases I worked on and the lack of type checking made things difficult. There's always Roblox-TS, but ideally we wouldn't need that abstraction.
from roblox-lua-promise.
Yea, you currently run into a few Luau limitations but can still get some pretty good checking for the first level of a Promise chain that has found some cool bugs for me. Here's the way I've made the best out of the current Luau type checking and syntax:
-- TODO Luau: workaround for Luau recursive type used with different parameters error. delete this copy once that feature is added.
export type _Thenable<R> = {
andThen: <U>(
self: _Thenable<R>,
onFulfill: (R) -> () | U,
onReject: (error: any) -> () | U
) -> (),
}
export type Thenable<R> = {
andThen: <U>(
self: Thenable<R>,
onFulfill: (R) -> () | _Thenable<U> | U,
onReject: (error: any) -> () | _Thenable<U> | U
-- TODO Luau: need union type packs to parse () | Thenable<U>
) -> nil | _Thenable<U>,
}
from roblox-lua-promise.
Looks like Luau supports type packs now: https://luau-lang.org/typecheck#type-packs
from roblox-lua-promise.
Hi! It's been a while since this issue was opened. Was it considered revisiting whether typing the library in Luau is doable yet?
from roblox-lua-promise.
It should be possible now with the improvements made to luau in the past year, assuming the new type solver is on by default.
from roblox-lua-promise.
Related Issues (20)
- Promise:finally counts as a consumer, but it probably shouldn't
- Queued callbacks from a cancelled sub-promise should not be run
- Little typo in :expect() documentation
- Use coroutine.close on cancelled promise threads
- Promise.fold does not return correct value if there is an unresolved Promise in the passed list
- Syntax error in documentation example
- Unhandled Promise Rejection warning HOT 6
- coroutine.close issue HOT 2
- retryWithDelay signature is wrong
- Stabilize source field
- Promise wally package throws error "Access is denied. (os error 5)" HOT 1
- `Promise.defer` should use `task.defer` HOT 3
- Cannot publish any library HOT 1
- Promise.all should not error for non-promise input values HOT 1
- Document the Error object and all of its members
- Promise.fromEvent as command HOT 1
- The auto-filling doesn't work? HOT 1
- Error in finally handler does not propagate to subsequent catch after a :timeout HOT 2
- Dependabot Alerts regarding `testez`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from roblox-lua-promise.