Typescript: declare return type to be dependent on argument value?

依然范特西╮ 提交于 2019-12-01 04:47:04

Yes, you can use overload on strings to do this. Example:

interface BarcodeScanner { scan(): number; }
interface Accelerometer { getAcceleration(): number; }

class MyClass {
    get(name: 'accelerometer'): Accelerometer;
    get(name: 'barcode'): BarcodeScanner;
    get(name: string): any; // Fallback string signature
    get(name: string): any { // Implementation signature, not visible
        /* ... */
        return undefined;
    }

    something() {
        let x = this.get('barcode');
        x.scan(); // OK
        let y = this.get('accelerometer');
        y.getAcceleration(); // OK
    }
}

It's not possible to statically determine a type in function of a (dynamic) string. But generics are made for these cases.

Example:

class MyClass {
    public get<T>(type: string): T {
        // ...
    }
    public anotherMethod() {
        this.get<Barcode>('barcode').scan();
    }
}

I'm not sure to understand the problem with Ember, but if the method get is dynamically inserted in the object, then maybe a solution like this:

class MyClass {
    public get: function<T> (type: string): T; // this is just a static declaration, there is no generated JS code here

    public anotherMethod() {
        this.get<Barcode>('barcode').scan();
    }
}

Yes, as of Typescript 3.2 you can use conditional types to do the trick:

const get = <T extends "barcode" | "mqtt">(s: T) =>
    s === "barcode" ?
        <T extends "barcode" ? {scan: () => string} : {pan: () => string}>{scan: () => "we are scanning"} :
        <T extends "barcode" ? {scan: () => string} : {pan: () => string}>{pan: () => "we are panning"}

get("barcode").scan() // OK
get("mqtt").pan()     // OK
get("barcode").pan() // Error

Note: I did not succeed in moving the casts to the function's return type - this will have to be put in another question for the real experts.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!