Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to apply a function interface which has properties as well?

How to apply the following interface?

interface Example<T> {
    (a: T): T;
    name: T | undefined;
}

const test: Example<string> = ???

I do not know what to assign to test. Have in mind I do not want to name the function included in the interface.
I found the above pattern in React:

interface FunctionComponent<P = {}> {
     (props: P, context?: any): ReactNode;
     propTypes?: WeakValidationMap<P> | undefined;
     contextTypes?: ValidationMap<any> | undefined;
     defaultProps?: Partial<P> | undefined;
     displayName?: string | undefined;
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The easiest way is to just do it in two steps, using the support for property declarations on functions:

const test: Example<string> = a => a.toUpperCase();
test.name = "hey"; 

But if you want to do it in a single line you can do so via Object.assign():

const test2: Example<string> =
  Object.assign((a: string) => a.toUpperCase(), { name: "hey" }); 

Playground link to code

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading