ckeditor5 icon indicating copy to clipboard operation
ckeditor5 copied to clipboard

Typings for TypeScript

Open ssougnez opened this issue 9 years ago • 53 comments

Hi,

I'm currently using ckeditor5 in TypeScript and I think it would be pretty usefull to ship typings definition. Is there something planed about this? I think it should be possible to auto generated them based on the JS files.

EDIT (by @Reinmar, 07.04.2023): It's out! 🚀 Read more in https://github.com/ckeditor/ckeditor5/issues/504#issuecomment-1499078025.

EDIT (by @filipsobol, 22.03.2023): The TypeScript migration is at the finish line. Alpha release of version 37 with typings is already out and being tested. We have updated our Vue integration and are in the process of updating React and Angular. You can follow the progress here: https://github.com/ckeditor/ckeditor5/issues/11704.

EDIT (by @Reinmar, 23.08.2022): It's happening. We're migrating to Typescript 🎉 Having official typings is the main goal of the migration.


If you'd like support for TypeScript to be introduced react with 👍 to this post.

ssougnez avatar Jul 17 '17 12:07 ssougnez

Quoting myself from https://github.com/ckeditor/ckeditor5/issues/347#issuecomment-309859172:

We maintain a very thorough API documentation (JSDoc-based notation) so it should not be hard to generate type definitions and maintain them in https://github.com/DefinitelyTyped/DefinitelyTyped.

I agree that it'd be great if typings were available. But we won't be able to work on that now. I wonder, though, how much work it requires, in general, to produce those typings out of JSDoc comments. The problem in our case may lay in the fact that JSDoc didn't work well for ES6 so we came out with https://www.npmjs.com/package/@ckeditor/jsdoc-plugins – a set of plugins which fix stuff after JSDoc and extend it to better work in our case.

Reinmar avatar Jul 18 '17 10:07 Reinmar

Typescript definitions would greatly help our cause of developing with ckeditor5 platform

nadavsinai avatar Jun 26 '18 11:06 nadavsinai

I've found only one good looking tool - https://github.com/englercj/tsd-jsdoc, but it doesn't support the module: syntax and we use it in a custom way...

And there're few problems to solve anyway.

  1. Events

JSDoc provides the synthetic @event and @fires, so all events and their documentation will be lost.

From the e.g.:


/**
 * Fired after {@link #initPlugins plugins are initialized}.
 *
 * @event pluginsReady
 */

We'd have to create such declarations:

declare class Emitter<EventMap> {
	listenTo<T extends keyof EventMap>( emitter: Emitter<EventMap>, event: T, callback: ( event: EventInfo<EventMap[ T ]> ) => void, options: any ): void;
	...
}

declare class Editor extends Emitter<EditorEvents> {

}

interface EditorEvents {
	pluginsReady: PluginsReadyEvent;
}

/**
 * Fired after {@link #initPlugins plugins are initialized}.
 */
interface PluginsReadyEvent {}

interface EventInfo<T> {}

to not lose the informations about the event. (Then, with every new editor event only the EditorEvents interface needs to be changed). I don't think that any JSDoc->TS plugin can support such syntax soon.

  1. Builds

We'd need to provide typings for every source file and for builds. We'd need somehow to combine type declarations, so the builds will include only one TS declaration file with all proper typings.

I agree, that these typings would be super helpful for someone using TS or IDE that supports declaration files. But it feels super hard to implement a tool for such conversion, IMO writhing declaration files by hand wisely would be easier as the API is established.

ma2ciek avatar Jul 27 '18 12:07 ma2ciek

I can see that only reasonable choice would be to use that lib: https://github.com/englercj/tsd-jsdoc. It operates on the JSDoc output, but it doesn't support many things, e.g. the module: syntax, so we'd need to somehow patch this tool and align to our needs.

ma2ciek avatar Sep 27 '18 15:09 ma2ciek

I see that I overcomplicate this issue in the https://github.com/ckeditor/ckeditor5/issues/504#issuecomment-408404900. If we'd like to make this tool we should implement it incrementally and make the TS output backward-compatible.

ma2ciek avatar Sep 27 '18 16:09 ma2ciek

@ma2ciek I know you made some work on running our source code through TypeScript compiler and using current documentation as type definitions. Could you write here a short summary? Is that feasible? How much work would it be needed?

Reinmar avatar Jan 03 '19 10:01 Reinmar

To sum up that thread, it's possible to use TS compiler to check our JavaScript code by taking advantage of JSDoc our comments. So once our code whole codebase will be type-checked it should be possible to generate proper TS declaration files for every file. But note that the PR that allows generating declaration files from JS code is still an open issue and the TS team is waiting for the community PRs (we can also try the mentioned dts-gen project but IMO it'd be better to wait for the official option).

According to changes needed to bring that type-checking and be somehow TS-compatible, we'd to need to work on:

  • converting all module: syntax to normal imports (I assume that the ESlint might complain about unused imports, but maybe there's some plugin for that, dunno) or to the relative import() syntax and add some changes to our JSDoc plugins so it could work properly with that syntax (not sure how much work it'd be needed)
  • checking whether our mixins could be understandable by TS - TS supports only single inheritance, but there could be some ways to mimic that effect.
  • checking whether the caught errors from JS files are highlighted in all IDEs we use.
  • fix all incorrect typedefs, interfaces, types, etc. in the existing code (like that one).

It's hard to tell exactly how much work is needed, IMO it's better to check all potential blockers first.

ma2ciek avatar Jan 03 '19 11:01 ma2ciek

  • checking whether our mixins could be understandable by TS

Shouldn't the interfaces be used for mixins? Instead of

/**
 * @mixes MixinFoo
 * @mixes MixinBar
 */

write:

/**
 * @implements InterfaceFoo
 * @implements InterfaceBar
 */

ps.: TS docs about mixins: https://www.typescriptlang.org/docs/handbook/mixins.html

jodator avatar Jan 03 '19 13:01 jodator

@jodator, we could use just interfaces, but then the class would need to implement all these interfaces anyway (Otherwise TS will complain). So that's not an option IMO. Yep, I've seen that part about mixins, but we can't do that in many cases as it's massive code duplication (we'd need to mock every property and method for every interface that we need to implement).

ma2ciek avatar Jan 03 '19 14:01 ma2ciek

it will be great to have typescript definition for ckeditor5 platform

medmor avatar Sep 01 '19 17:09 medmor

it will be great to have typescript definition for ckeditor5 platform. me too

andysanti avatar Sep 05 '19 22:09 andysanti

To sum up that thread, it's possible to use TS compiler to check our JavaScript code by taking advantage of JSDoc our comments. So once our code whole codebase will be type-checked it should be possible to generate proper TS declaration files for every file. But note that the PR that allows generating declaration files from JS code is still an open issue and the TS team is waiting for the community PRs (we can also try the mentioned dts-gen project but IMO it'd be better to wait for the official option).

[...]

Just stumbled upon this. FYI the issue about allowing generation of typing from JavaScript was merged a few weeks ago, although it looks like it wasn't officially released yet. It'll probably be included in the 3.7 release. (TypeScript 3.7 Iteration Plan)

ghost avatar Oct 17 '19 14:10 ghost

Please/ TypeScript support is very important for use in project. Please implement base Types support

CrazyAdmin avatar Oct 23 '19 07:10 CrazyAdmin

Do you have any plans of rewriting this in Typescript?

IonelLupu avatar Jan 23 '20 12:01 IonelLupu

Hi @IonelLupu,

There're plans to either rewrite to TS or to use JSDoc annotations and generate declaration files out of them, but I can't guarantee when it happens and if it happens.

See https://github.com/ckeditor/ckeditor5/issues/1415.

ma2ciek avatar Jan 23 '20 13:01 ma2ciek

@Reinmar @ma2ciek

I recently developed a plugin for CKEditor5 and found that it is really inconvenient to not have TS type files. Although CKEditor5 has a very complete API manual, but the manual is not as convenient and timely as TS type. I know that rewriting CKEditor5 with TS is very difficult, but I think If you want to write a TS type, it is relatively simple, even if all types use any, so I know at least those Methods or Properties are available. thank you for developing such a good and easy-to-use Editor. Grateful!

SolidZORO avatar Feb 14 '20 06:02 SolidZORO

自己写 type 真的很花时间,最主要的问题还是自己不一定能写对!

下面这个 writer 蛮常用的,前后整理核对下来花了 2h。😭

// @ckeditor/ckeditor5-engine/src/model/writer
  class Writer {
    readonly batch: any;
    readonly model: model.Model;
    //
    addMarker(name: string, options: { usingOperation: boolean; range: Range; affectsData?: boolean }): Marker;
    append(item: Item | DocumentFragment, parent: Element | DocumentFragment);
    appendElement(
      name: name,
      attributes?: { [key: string]: string | RegExp | boolean },
      parent: Element | DocumentFragment,
    );
    appendText(
      text: string,
      attributes?: { [key: string]: string | RegExp | boolean },
      parent: Element | DocumentFragment,
    );
    clearAttributes(itemOrRange: Item | Range);
    createDocumentFragment(): DocumentFragment;
    createElement(name: string, attributes?: { [key: string]: string | RegExp | boolean }): Element;
    createPositionAfter(item: Item): Position;
    createPositionAt(itemOrPosition: Item | Position, offset?: number | 'end' | 'before' | 'after'): Position;
    createPositionBefore(item: Item): Position;
    createPositionFromPath(root: Element | DocumentFragment, path: number[], stickiness?: PositionStickiness): Position;
    createRange(start: Position, end?: Position): Range;
    createRangeIn(element: Element): Range;
    createRangeOn(element: Element): Range;
    createSelection(
      selectable: Selection,
      placeOrOffset?: number | 'before' | 'end' | 'after' | 'on' | 'in',
      options?: { backward?: boolean },
    ): Selection;
    createText(data: string, attributes?: { [key: string]: string | RegExp | boolean }): Text;
    insert(item: Selection, itemOrPosition: Item | Position, offset?: number | 'end' | 'before' | 'after');
    insertElement(name, attributes?: { [key: string]: string | RegExp | boolean }, itemOrPosition, [offset]);
    insertText(data, attributes?: { [key: string]: string | RegExp | boolean }, itemOrPosition, [offset]);
    merge(position: Position);
    move(range: Range, itemOrPosition: Item | Position, offset?: number | 'end' | 'before' | 'after');
    overrideSelectionGravity(): string;
    remove(itemOrRange: Item | Range);
    removeAttribute(key: string, itemOrRange: Item | Range);
    removeMarker(markerOrName: Marker | string);
    removeSelectionAttribute(keyOrIterableOfKeys: string | string[]);
    rename(element: Element, newName: string);
    restoreSelectionGravity(uid: string);
    setAttribute(key: string, value: any, itemOrRange: Item | Range);
    setAttributes(attributes: { [key: string]: string | RegExp | boolean }, itemOrRange: Item | Range);
    setSelection(
      selectable: Selection,
      placeOrOffset: number | 'before' | 'end' | 'after' | 'on' | 'in',
      options?: { backward?: boolean },
    );
    setSelectionAttribute(keyOrObjectOrIterable: any, value: any);
    setSelectionFocus(itemOrPosition: Item | Position, offset: number | 'before' | 'end' | 'after');
    split(position: Position, limitElement: Node): any;
    unwrap(element: Element);
    updateMarker(
      markerOrName: string | Marker,
      options?: { range?: Range; usingOperation?: boolean; affectsData?: boolean },
    );
    wrap(range: Range, elementOrString: Element | string);
  }

SolidZORO avatar Feb 22 '20 09:02 SolidZORO

Any news ? 😃

pierre-H avatar Mar 30 '20 21:03 pierre-H

We want typings, we want typings, we want Typings

Anamican avatar Apr 22 '20 06:04 Anamican

Holy obvious omission batman!

This is a pretty big flaw to not address this as of 2020... For a professional package, that I'm expecting to pay for, typescript types are feature #1 that I would need.

It's also a sniff test... if you guys are missing this feature it's not a good sign for other features that I would expect.

InputNeuron avatar May 28 '20 14:05 InputNeuron