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;
}
>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" });