TypeScript require generic parameter to be provided

前端 未结 4 1109
长发绾君心
长发绾君心 2021-01-07 20:54

I have the following function:

async function get(url: string): Promise {
    return getUrl(url);
}

However, i

4条回答
  •  执念已碎
    2021-01-07 21:02

    You could use multiple type parameters:

    function contractType(value: unknown): U {
        return value as U
    }
    
    const example1: string = contractType(17) // error
    const example2: string = contractType("value") // error
    const example3: string = contractType("value") // ok
    

    https://github.com/Microsoft/TypeScript/issues/14829#issuecomment-288902999

提交回复
热议问题