LuaLS / LuaLS/lua-language-server

How do I document a generic class (JS Promises)

Aperta
#1,532 4 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

enhancement feat/generic
Lingua principale
Lua
Stelle
4.4k
Fork
442
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

Hello everyone! First of all, thank you for the amazing work on this server. It's really great and extremelly fast to use!

I've a question about how can I use the type system to properly type a Promise-like table that I'm creating. Right now I need to type this piece of code:

local Promise = require ".../promise"
Promise:new(function(resolve, reject)
  -- do some async stuff...
  if some_async_true_status then
    resolve(some_async_result)
  else
    reject(some_async_error)
  end
end):next(function(result) -- < `resolve()` value here
  print(result)
end):catch(function(err) -- < `reject()` value here
  print(err)
end)

This a Promise-like implementation that I'm doing for my neovim plugin which I'm trying to get a similar API as the Javascript Promise.

My question here is, how can I type this to the callee function knows what this is going to be returned from that callback? Currently, I'm trying to play around with @generic but, I can't get the server to properly understand what I mean by my types:

local Promise = {}

---@generic T
---@class Promise<T>
---@field new(executor: fun(resolve: fun(result: T), reject: fun(err: any)))): Promise
---@field next(fn: fun(result: T)): Promise <<<<< here I'm returning `self`
---@field catch(fn: fun(err: any)): Promise <<<<< here I'm returning `self` as well

function Promise:new(executor)
  local promise = { ... }
  -- ... some operations
  return setmetatable(promise, {
    __index = self,
  })
end

function Promise:next(fn)
  -- some operations...
  return self
end

function Promise:catch(fn)
  -- some operations...
  return self
end

----------------------
-- Here is how I want to use it:

---@return Promise<string>
function my_function_that_return_promise()
  return Promise:new(function(resolve, reject)
    -- .....
    resolve("hello")
  end)
end

my_function_that_return_promise():next(function(result)
  print(result) -- << here I want the server to show `result` is a `string`
end)

For reference, this is how we can do this exactly thing using typescript:

const myPromise = new Promise<string>((resolve, reject) => {
  // ...
  resolve("hello")
})

myPromise
  .then((result) => console.log(result)) // < `result` will have `string` type
  .catch((err) => console.log(err))

Thank you for the awesome work! Sorry if I didn't make myself clear, let me know if I need to provide any further information.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con le annotazioni @generic e @class nell'esempio di Promise e traccia il modo in cui il language server inferisce attualmente i tipi dei callback tramite new(), next() e catch(). Il lavoro è completo quando si determina se la propagazione dei generics richiesta è supportata o richiede una modifica definita al sistema dei tipi, con il risultato di tipo string inferito nel callback finale.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
lua, neovim
Ambito
devtools
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
20/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.