# Type definitions

**URL:** <https://discuss.prosemirror.net/t/type-definitions/97>\
**Category:** Uncategorized\
**Created:** [December 10, 2015, 8:28pm UTC](https://discuss.prosemirror.net/t/type-definitions/97 "2015-12-10T20:28:54Z")\
**Posts on this page:** 11\
**Page:** 2

<div class="post-metadata">

**Author:** ![davidka](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/davidka/32/429_2.png) [@davidka](https://discuss.prosemirror.net/u/davidka)\
**Post date:** [February 10, 2017, 8:51am UTC](https://discuss.prosemirror.net/t/type-definitions/97/21 "2017-02-10T08:51:37Z")

</div>

That’s great, thanks!

As I see there are some places (e.g. constructor’s) with type “any” although the type information is available:

```auto
  class Mark {
   constructor(type: any, attrs: any);
   type: MarkType;
   attrs: any;
   ...

```

How did you generate the file? From code comments? Maybe we can improve this a little before publishing to typing registry

---

<div class="post-metadata">

**Author:** ![timjb](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/timjb/32/558_2.png) [@timjb](https://discuss.prosemirror.net/u/timjb)\
**Post date:** [May 25, 2017, 1:13pm UTC](https://discuss.prosemirror.net/t/type-definitions/97/22 "2017-05-25T13:13:18Z")

</div>

I’m currently working on a project using ProseMirror with TypeScript. I’ve noticed that the bindings available through DefinitelyTyped are outdated (they describe version 0.18 of ProseMirror) and also inaccurate in some places.

Some of the inaccuracies can be fixed by improving the generator that takes the `getdocs` documentation and produces the type declarations:

- API calls that may return `null` or `undefined` are marked with a `?` in front of the return type (e.g. `maybeChild(index: number) → ?Node`) in the ProseMirror documentation. The TypeScript declarations currently lack this information (`maybeChild(index: number): ProsemirrorNode`). Using the `null` and `undefined` types introduced in [TypeScript 2.0](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html) we could give this function the more correct signature `maybeChild(index: number): ProsemirrorNode | undefined | null`. Looking at the implementation of `maybeChild`, we can see that `maybeChild` may return `undefined`, but does not return `null`. So the most accurate type would be `maybeChild(index: number): ProsemirrorNode | undefined` in this case. But this can’t be deduced from the getdocs annotations in their current form.
- Types of the form `Object<SomeType>` that describe javascript objects whose values are of the type `SomeType` aren’t translated correctly. For example, the `nodes` member of the `Schema` class has the annotation `nodes: Object;` instead of the more accurate `nodes: { [name: string]: NodeType };`

Then there are some corrections that can only done manually:

- The `DOMOutputSpec` interface described in prose in the documentation can be manually translated to a TypeScript definition:

```auto
   interface DOMOutputSpecArray { interface DOMOutputSpecArray {
      0: string, 0: string,
      1?: DOMOutputSpec | 0 | { [attr: string]: string }, + 1?: DOMOutputSpec | 0 | Object,
      2?: DOMOutputSpec | 0, 2?: DOMOutputSpec | 0,
      3?: DOMOutputSpec | 0, 3?: DOMOutputSpec | 0,
      4?: DOMOutputSpec | 0, 4?: DOMOutputSpec | 0,
     5?: DOMOutputSpec | 0, 5?: DOMOutputSpec | 0,
     6?: DOMOutputSpec | 0, 6?: DOMOutputSpec | 0,
     7?: DOMOutputSpec | 0, 7?: DOMOutputSpec | 0,
     8?: DOMOutputSpec | 0, 8?: DOMOutputSpec | 0,
     9?: DOMOutputSpec | 0 9?: DOMOutputSpec | 0
   } }
   type DOMOutputSpec type DOMOutputSpec
       = string = string
       | Node | Node
       | DOMOutputSpecArray | DOMOutputSpecArray

```

- Many methods of `Transform` (e.g. `clearMarkup(from: number, to: number) → Transform`) return the object itself, so you can build transforms by chaining calls together. When you call these methods on the child class `Transaction`, you will obviously get a `Transaction` back. But the TypeScript signature is `clearMarkup(from: number, to: number): Transform`, so TypeScript thinks that you only get a transform back. Using [polymorphic this types](https://www.typescriptlang.org/docs/handbook/advanced-types.html#polymorphic-this-types), a type introduced for chainable APIs, we can give `clearMarkup` the declaration `clearMarkup(from: number, to: number): this`. Then TypeScript will be able to infer that `someTransaction.clearMarkup(from, to)` is still a transaction, not only a transform.

I would like to help bringing the type declarations up to date and fixing these inaccuracies. The bindings currently published on DefinitelyTyped to to have been generated using [davidka/buildtypedefs ([https://github.com/davidka/buildtypedefs](https://github.com/davidka/buildtypedefs)). Is that correct, @davidka? (Are there any instructions on how to produce the type definitions using your tool somewhere?) I think that we should fix the first group of inaccuracies in the generator, rerun the translator on the current version of ProseMirror and then check the generated type declarations manually and fix/enhance inaccurate ones.

Maybe we can add some option to the generator that causes it to use handwritten definitions (exporting them from some `.d.ts` file) instead of generating its own for selected interfaces/types. That way, we could save ourselves the effort of having to manually edit some auto-generated file every time there is a new release of ProseMirror. On the other hand @marijn has said that there won’t be any significant API changes at least before 1.0, so maybe it is less effort to just keep them up to date manually.

---

<div class="post-metadata">

**Author:** ![vandenoever](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/vandenoever/32/322_2.png) [@vandenoever](https://discuss.prosemirror.net/u/vandenoever)\
**Post date:** [May 27, 2017, 7:36pm UTC](https://discuss.prosemirror.net/t/type-definitions/97/23 "2017-05-27T19:36:46Z")

</div>

The 0.18 types at DefinitelyTyped are working well for getting started. I’ll probably keep using them, because they cut down on trial-and-error while developing.

---

<div class="post-metadata">

**Author:** ![bradleyayers](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/bradleyayers/32/350_2.png) [@bradleyayers](https://discuss.prosemirror.net/u/bradleyayers)\
**Post date:** [May 30, 2017, 5:10am UTC](https://discuss.prosemirror.net/t/type-definitions/97/24 "2017-05-30T05:10:24Z")

</div>

I’m working on updating the types on DefinitelyTyped based on your work @timjb. [https://github.com/DefinitelyTyped/DefinitelyTyped/pulls/bradleyayers](https://github.com/DefinitelyTyped/DefinitelyTyped/pulls/bradleyayers)

I’m using [https://github.com/bradleyayers/generate-prosemirror-types](https://github.com/bradleyayers/generate-prosemirror-types) as a bit of scaffolding to streamline the process.

I’ve raised some issues that overlap with some of the issues you’ve described, and will have a go at fixing them.

I’m doing a combination of using getdocs2ts (forked from your `refactor` branch of `buildtypedefs`) and manually making changes so that we can have types in the short term, while improve the tool in the longer term.

---

<div class="post-metadata">

**Author:** ![timjb](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/timjb/32/558_2.png) [@timjb](https://discuss.prosemirror.net/u/timjb)\
**Post date:** [May 30, 2017, 9:56am UTC](https://discuss.prosemirror.net/t/type-definitions/97/25 "2017-05-30T09:56:54Z")

</div>

Hey @bradleyayers! I would like to be able to generate the type signatures fully automatically in the future, without manual editing required. Therefore it would be good to have a list of things that you have fixed manually while working on the definitions for 0.21, so we can work on automating them. If there is incorrect documentation (I have found some while working on this), it’s best to fix it upstream. I also want to make it possible to have the generator use custom definitions for some declarations/functions/etc. This way we could use the custom interface declaration for `DOMOutputSpec` for example.

---

<div class="post-metadata">

**Author:** ![bradleyayers](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/bradleyayers/32/350_2.png) [@bradleyayers](https://discuss.prosemirror.net/u/bradleyayers)\
**Post date:** [May 30, 2017, 11:38am UTC](https://discuss.prosemirror.net/t/type-definitions/97/26 "2017-05-30T11:38:07Z")

</div>

Sounds great! I’ve been recording the manual tweaks in the github issues on [https://github.com/bradleyayers/getdocs2ts](https://github.com/bradleyayers/getdocs2ts), there’s some missing like the “polymorphic this types” that you described, so feel free to raise those.

---

<div class="post-metadata">

**Author:** ![davidka](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/davidka/32/429_2.png) [@davidka](https://discuss.prosemirror.net/u/davidka)\
**Post date:** [June 3, 2017, 5:27am UTC](https://discuss.prosemirror.net/t/type-definitions/97/27 "2017-06-03T05:27:04Z")

</div>

@timjb @bradleyayers Great Job! My initial idea was to provide the type-generator to @marijnso so he can include this as an additional build step and include the typings with each release of prosemirror. To provide the \*.d.ts files with prosemirror packages itself would remove the necessity to update it each time on DefinitelyTyped

---

<div class="post-metadata">

**Author:** ![bradleyayers](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/bradleyayers/32/350_2.png) [@bradleyayers](https://discuss.prosemirror.net/u/bradleyayers)\
**Post date:** [June 16, 2017, 3:58am UTC](https://discuss.prosemirror.net/t/type-definitions/97/28 "2017-06-16T03:58:55Z")

</div>

[https://github.com/DefinitelyTyped/DefinitelyTyped/pull/16981](https://github.com/DefinitelyTyped/DefinitelyTyped/pull/16981) has been merged, so hopefully we’ll have some 0.21 types out on NPM soon. No doubt there’ll probably be some refinement over time, but it’s a great step forward!

---

<div class="post-metadata">

**Author:** ![Keats](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/keats/32/373_2.png) [@Keats](https://discuss.prosemirror.net/u/Keats)\
**Post date:** [August 14, 2017, 12:27pm UTC](https://discuss.prosemirror.net/t/type-definitions/97/29 "2017-08-14T12:27:12Z")

</div>

If we want to make some changes, should we do them on definitelytyped directly? How does it work to keep track of the changes if the types are automatically generated?

For example, I’d like to add a type for `(state: EditorState, dispatch?: (tr: Transaction) => void): boolean;` in `prosemirror-commands` rather than repeating that everywhere.

---

<div class="post-metadata">

**Author:** ![bradleyayers](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/bradleyayers/32/350_2.png) [@bradleyayers](https://discuss.prosemirror.net/u/bradleyayers)\
**Post date:** [August 14, 2017, 9:46pm UTC](https://discuss.prosemirror.net/t/type-definitions/97/30 "2017-08-14T21:46:08Z")

</div>

Yes just raise a pull request to DefinitelyTyped, and be sure to add a test for it. The tests aren’t automatically generated (and at this point what’s in DefinitelyTyped isn’t either, it’s hand-tweaked after auto-generation) so they serve as a good mechanism for avoiding regressions.

---

<div class="post-metadata">

**Author:** ![Alecyrus](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/alecyrus/32/842_2.png) [@Alecyrus](https://discuss.prosemirror.net/u/Alecyrus)\
**Post date:** [January 9, 2019, 5:17am UTC](https://discuss.prosemirror.net/t/type-definitions/97/31 "2019-01-09T05:17:15Z")

</div>

Any updade?

[Previous page](https://discuss.prosemirror.net/t/type-definitions/97.md?page=1)
