Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 142440

Argument of function is a property that exists in a type of Typescript

$
0
0

I want to write a function that returns a single property that exists on a type:

type CustomType = {
    property1: boolean;
    property2: numeric;
};

private getData(obj): CustomType {
    // do stuff
    return dataObj;
}

private getBooleanValue(obj, key): boolean {
    const value = this.getData(obj)[key];
    // do stuff
    return value;
}

I want to put a restriction on getBooleanValue's key, that the key is part of CustomType - for example:

getBooleanValue(obj, "property1") // OK
getBooleanValue(obj, "property2") // ERROR, TypeScript won't allow this

Viewing all articles
Browse latest Browse all 142440


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>