englercj / resource-loader

A middleware-style generic resource loader built with web games in mind.
http://englercj.github.io/resource-loader/
MIT License
424 stars 77 forks source link

Typescript definition file #118

Closed goldenratio closed 5 years ago

goldenratio commented 5 years ago

Hi all,

If someone has typescript d.ts file, can you please share?

Thanks.

englercj commented 5 years ago

I've updated the JSDoc comments and my typescript generator to bring you this:

https://github.com/englercj/resource-loader/tree/master/typings

If you encounter any issues, let me know.

goldenratio commented 5 years ago

@englercj Hi Chad, Thanks for d.ts

I am getting following issues..


node_modules/resource-loader/typings/resource-loader.d.ts:11:19 - error TS2709: Cannot use namespace 'async' as a type.

11     static async: async;
                     ~~~~~

node_modules/resource-loader/typings/resource-loader.d.ts:12:26 - error TS2304: Cannot find name 'encodeBinary'.

12     static encodeBinary: encodeBinary;
                            ~~~~~~~~~~~~

node_modules/resource-loader/typings/resource-loader.d.ts:13:20 - error TS2304: Cannot find name 'encodeBinary'.

13     static base64: encodeBinary;
                      ~~~~~~~~~~~~

node_modules/resource-loader/typings/resource-loader.d.ts:21:17 - error TS2304: Cannot find name 'Signal'.

21     onProgress: Signal;
                   ~~~~~~

Following changes needs to be done, to make it work.

https://github.com/englercj/resource-loader/blob/master/typings/resource-loader.d.ts#L11

static async: typeof async;
static encodeBinary: typeof encodeBinary;
static base64: typeof encodeBinary;

https://github.com/englercj/resource-loader/blob/master/typings/resource-loader.d.ts#L90

load(cb?: Loader.OnCompleteSignal): void;

and also could include Signal types inside Loader namespace.

Here is my handwritten d.ts for reference, https://gist.github.com/goldenratio/4ec8441b21ac2a80d8f58b7bc12b60fe

englercj commented 5 years ago

I'm still trying to figure out how to annotate in jsdoc that a type should be typeof. Haven't figured that one out. If you know if JSDoc has a syntax to distinguish between an instance and typeof, let me know.

I'll make the change to fix the OnCompleteSignal, some of the Resource callbacks aren't getting set correctly.

As for signals, I think the typings here should import it but not define it since it is a separate module. I'll try to fix that up as well.

englercj commented 5 years ago

I've fixed up the typeof and OnCompleteSignal problems, but still thinking about how to solve the Signal one.

goldenratio commented 5 years ago

btw, shouldn't Signal be MiniSignal. https://github.com/Hypercubed/mini-signals/blob/master/typings/mini-signals.d.ts I see that we are using default import in Resource.js

Edit: about external modules in d.ts, probably triple-slash directive will work? /// <reference path="..." />

goldenratio commented 5 years ago

triple-slash directive will not work. I think we might need to need include MiniSgnal types inside resource-loader.d.ts

englercj commented 5 years ago

Yeah I was going to experiment with using import directives and see if that let typescript resolve the files correctly through the mini-signals project. Not sure if it will work, and if not I'll put them in myself.

goldenratio commented 5 years ago

@englercj also add function is missing multiple signatures..

// tslint:disable-next-line:no-any
add(...params: any[]): Loader;

add(name: string, url: string, options?: LoaderOptions, cb?: () => void): Loader;

add(url: string, options?: LoaderOptions, cb?: () => void): Loader;
englercj commented 5 years ago

Yeah, again JSDoc doesn't really have a way to do overloads. So instead I picked a permissive one that contains most of them. If you can figure out how to make overloads show up in JSDoc, please update it.

goldenratio commented 5 years ago

function overloading seems to work in JSDoc. Try this please..

    /**
     * @function
     * @variation 1
     * @param {Array<string>} urls Array containing urls
     * @return {this} Returns itself.
     *//**
     * @function
     * @variation 2
     * @param {string|object|any[]} obj 
     * @param {IAddOptions} [options] - The options for the load.
     * @param {Loader.OnCompleteSignal} [cb] - Function to call when this specific resource completes loading.
     * @return {this} Returns itself.
     *//**
     * @function
     * @variation 3
     * @param {string} name - The name of the resource to load, if not passed the url is used.
     * @param {string} url - The url for this resource, relative to the baseUrl of this loader.
     * @param {IAddOptions} [options] - The options for the load.
     * @param {Loader.OnCompleteSignal} [cb] - Function to call when this specific resource completes loading.
     * @return {this} Returns itself.
     */

It generates,

add(urls: string[]): this;
add(obj: string | any | any[], options?: IAddOptions, cb?: Loader.OnCompleteSignal): this;
add(name: string, url: string, options?: IAddOptions, cb?: Loader.OnCompleteSignal): this;

I can create a PR when I get home.

englercj commented 5 years ago

I tried something similar to that before and didn't get what I expected, but if it is working for you, maybe you found the secret sauce. I'd love a PR!

Edit: Ah I see, I didn't do the @variation tag. That must be the secret sauce.

goldenratio commented 5 years ago

@englercj please check https://github.com/englercj/resource-loader/pull/126

goldenratio commented 5 years ago

@englercj I am seeing below errors now.. I will investigate soon

node_modules/@gsbaltic/resource-loader/typings/resource-loader.d.ts:23:17 - error TS2315: Type 'MiniSignal' is not generic.

23     onProgress: Signal<Loader.OnProgressSignal>;
                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/@gsbaltic/resource-loader/typings/resource-loader.d.ts:24:14 - error TS2315: Type 'MiniSignal' is not generic.

24     onError: Signal<Loader.OnErrorSignal>;
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/@gsbaltic/resource-loader/typings/resource-loader.d.ts:25:13 - error TS2315: Type 'MiniSignal' is not generic.

25     onLoad: Signal<Loader.OnLoadSignal>;
               ~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/@gsbaltic/resource-loader/typings/resource-loader.d.ts:26:14 - error TS2315: Type 'MiniSignal' is not generic.

26     onStart: Signal<Loader.OnStartSignal>;
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/@gsbaltic/resource-loader/typings/resource-loader.d.ts:27:17 - error TS2315: Type 'MiniSignal' is not generic.

27     onComplete: Signal<Loader.OnCompleteSignal>;
                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/@gsbaltic/resource-loader/typings/resource-loader.d.ts:89:14 - error TS2315: Type 'MiniSignal' is not generic.

89     onStart: Signal<Resource.OnStartSignal>;
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/@gsbaltic/resource-loader/typings/resource-loader.d.ts:90:17 - error TS2315: Type 'MiniSignal' is not generic.

90     onProgress: Signal<Resource.OnProgressSignal>;
                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/@gsbaltic/resource-loader/typings/resource-loader.d.ts:91:17 - error TS2315: Type 'MiniSignal' is not generic.

91     onComplete: Signal<Resource.OnCompleteSignal>;
                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/@gsbaltic/resource-loader/typings/resource-loader.d.ts:92:24 - error TS2315: Type 'MiniSignal' is not generic.

92     onAfterMiddleware: Signal<Resource.OnCompleteSignal>;
                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
englercj commented 5 years ago

You probably included multiple type definitions for "mini-signals". The others are not generic, but the ones provided here are.