vilicvane / clime

⌨ The command-line interface framework for TypeScript.
252 stars 10 forks source link

StringCastable example from README not working #44

Closed plu closed 6 years ago

plu commented 6 years ago
export class File implements StringCastable<File> {
    constructor(public path: string) {}

    // This is required by the StringCastable interface, but it's never called.
    cast(source: string, context: CastingContext<File>): Promise<File> {
        console.log('This is never called');
        return new Promise(() => {});
    }

    // But this is actually called.
    static cast(source: string, context: CastingContext<File>): Promise<File> {
        console.log('This is actually called');
        return new Promise((resolve, reject) => {
            resolve(new File(Path.resolve(context.cwd, source)));
        });
    }
}

I'm wondering if I'm doing something wrong here: The StringCastable forces me to implement cast(source: string, context: CastingContext<File>): Promise<File>, which is then never even called. The static one is actually being called. And unless I implement the static one, I get a runtime error:

Error: TypeFilecannot be casted from a string, seeStringCastableinterface for more information

vilicvane commented 6 years ago

Thanks for addressing this. StringCastable should be the type of the constructor instead of the instance. So implements StringCastable should be removed from the example.

vilicvane commented 6 years ago

Fixed https://github.com/vilic/clime/commit/30e427a9765102cf3b99f3790119cf8e21e9f53d

plu commented 6 years ago

Thank you, this wasn't clear to me, I'm still new to TS.

I really love clime and the API it provides, great work you have done here!