简体   繁体   中英

About typescript interface definition

I have a function A which will return function B. The param of function B is object C. C has a property called D whose type is T.

The T is decided when I get B which means I could set T when I call A or some other ways.

So how to define it in typescript? Thanks so much.


I've tried this which will work. But that's not what I want:

interface C<T> {
    d: T;
    e: number;
}

interface B<T> {
    (param: C<T>): void;
}

interface A<T> {
    (): B<T>;
}

const a: A<number> = () => ({d, e}) => {
    console.log(d, e)
};

The things I want maybe something like:

const a: A = () => ({d, e}) => {
    console.log(d, e)
};
const b1 = a<number>();
const b2 = a<string>();

I have no idea about this.

You are on the right path, I find it cleaner with types rather than interfaces :

interface C<T> {
    d: T;
    e: number;
}

type B<T> = (params: C<T>) => void

type A = <T>() => B<T>

// or inlined : type A = <T>() => (params: C<T>) => void

const a: A = () => ({d, e}) => {
    console.log(d, e)
};

const withNumber = a<number>();
const withString = a<string>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM