Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #41479

Re: TypeScript JSDoc generic member function

From Julio Di Egidio <julio@diegidio.name>
Newsgroups comp.lang.javascript
Subject Re: TypeScript JSDoc generic member function
Date 2023-10-26 16:25 +0200
Organization A noiseless patient Spider
Message-ID <uhdst7$1l4pq$1@dont-email.me> (permalink)
References <uhbl9k$u0u5$1@dont-email.me>

Show all headers | View raw


On 25/10/2023 20:03, Julio Di Egidio wrote:
> I cannot seem to find a way to tell TypeScript with JSDoc that a
> member function of some object/interface takes a type parameter.
> 
> Here is a minimal example illustrating the problem:
> 
> ```js
> /**
>   * @template {unknown} T
>   * @callback IGet
>   * @param {string} key
>   * @returns {T | undefined}
>   */
> 
> /**
>   * @typedef IGetter
>   * @type {object}
>       * _@template {unknown} T  // how to write this?
>   * @property {IGet<T>} get
>   */
> ```
> 
> Error: Cannot find name 'T'.
> 
> That of course can be done in TypeScript, but how to do it with
> [TypeScript's] JSDoc?  Is it at all possible?

I was wrong, that cannot be done in TypeScript either...

Eventually, the only way I have found is to repeat the
declaration in-line, as in:

```js
/**
  * @typedef IGetter
  * @type {object}
  * @property {<T extends unknown>(key: string) => (T | undefined)} get
  */
```

but I lose any chance to properly document that function, which
is simply quite a disaster: and not the only one, also the return
type gets messed up (becomes just `T`), to the point that I am
compelled to do something altogether different, i.e. have a
concrete object constructor.

File under the misery of an entire industry.

Julio

Back to comp.lang.javascript | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

TypeScript JSDoc generic member function Julio Di Egidio <julio@diegidio.name> - 2023-10-25 20:03 +0200
  Re: TypeScript JSDoc generic member function John Harris <niam@jghnorth.org.uk.invalid> - 2023-10-26 11:28 +0100
  Re: TypeScript JSDoc generic member function Julio Di Egidio <julio@diegidio.name> - 2023-10-26 16:25 +0200

csiph-web