N
N
netW0rm2017-03-05 12:27:14
typescript
netW0rm, 2017-03-05 12:27:14

How to define interface in typescript?

Let's say there is such a proxy

let test = new Proxy(new String('test'), {
    get: (str, method, r) => (...args: any[]) => new Promise(resolve => {
        resolve(str[method](...args))
    })
})

Now you can call methods of the String object and the result will be returned in promises
let p = test.indexOf('st')
p.then(n => console.log(n)) // 2
test.toUpperCase().then(s => console.log(s)) // TEST

But typscript doesn't understand that the methods indexOf, toUpperCase etc. called through test return promises.
How to define such an interface so that typscript understands the type correctly?
interface PromiseOfString {
    // define
}

let test: PromiseOfString = new Proxy(new String('test'), {
    get: (str, method, r) => (...args: any[]) => new Promise(resolve => {
        resolve(str[method](...args))
    })
})

let p = test.indexOf('est')

typescript playground
In this example, the variable pmust have a type. Promise<number>
Of course, you can stupidly write all the methods in the interface with your hands, but this is stupid

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question