'How can I assign a value to a variable of a callable type?
How can I assign a variable of a callable type (type with call signature)?
interface CallableType<T> {
(isSomething: boolean): T
description: string
}
const fn: CallableType<number> = ?
const result = fn(false)
How can I assign a value to fn so that it can have a property description and it's callable at the same time?
Solution 1:[1]
A shorter solution would be to use Object.assign:
// fn is (<T>(x: T) => T) & { description: string }
const fn = Object.assign(<T>(x: T) => x, { description: 'description here' });
Solution 2:[2]
I found this way but maybe not the best
interface CallableType<T> {
(x: T): T
description: string
}
let foo = <T>(x: T) => x
const fn = foo as CallableType<string>
fn.description = 'description here'
console.log(fn('some string'))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | hittingonme |
| Solution 2 | modex98 |
