mind-elixir-core icon indicating copy to clipboard operation
mind-elixir-core copied to clipboard

typescript 支持

Open AshenBird opened this issue 3 years ago • 2 comments

能否再在发布的包中提供类型信息

AshenBird avatar Nov 11 '21 06:11 AshenBird

其实类和实例的类型还在想怎么定义比较好,现在用 prototype 不太方便

SSShooter avatar Nov 11 '21 06:11 SSShooter

其实类和实例的类型还在想怎么定义比较好,现在用 prototype 不太方便

这是个问题,这种方式确实不太利于类型推导,我比较少采用这种方式,是不是可以为采用这种方式的位置使用断言,来辅助类型推导。

AshenBird avatar Nov 11 '21 07:11 AshenBird

If I understand correctly, is it true that types are not provided because the implementation may change from using prototypes to something else? (Not I understand due to language barrier). I think in any case, it would be good to type at least the external API.

Currently, I have had to manually copy and paste the contents of ./types/mind-elixir/{dom,function,index}.d.ts so I could define my own types for the package:

module 'mind-elixir' {
	declare function findEle(id: string, instance?: any): Element

	export declare interface MindElixirOptions {
		/**
		 * The HTML Element controlled by MindElixir.
		 */
		el: string | HTMLDivElement

		/**
		 * The direction of <it>.
		 */
		direction: number

		/**
		 * Whether or not to make <it> draggable.
		 */
		draggable: boolean

		/**
		 * If <it> should add a context menu.
		 */
		contextMenu: boolean

		// ...
	}

	declare interface MindElixir {
		init(data: MindElixirData): void
	}
	declare var MindElixir: {
		new (options: MindElixirOptions): MindElixir
		_new(topic: string): MindElixirData

		LEFT: 0
		RIGHT: 1
		SIDE: 2
		version: string
	}

	export default MindElixir
	export declare var E = findEle
}

(Since MindElixir does MindElixir.new = function() { ... }, I wasn't sure how to describe that in TypeScript, which is why I used _new and had to do MindElixir._new = MindElixir.new in my own code.

Maybe this can be used as a starting point? I'm not the best at typing modules with TypeScript, but I'm sure others can improve this over time. I really like MindElixir and I think bundling types with the library can really improve the development experience.

hyperupcall avatar Jun 26 '23 17:06 hyperupcall

d.ts files were not extracted from logic code when this issue was created. i think this issue can be solved easier than before, but i haven't study how to export d.ts to npm package.

SSShooter avatar Jun 27 '23 02:06 SSShooter