我希望为泛型提供默认值将优先于类型推断,但事实并非如此:
// placeholder for a real express response.send
const sendResponse = (x) => console.log(x);
function sendResult<T = never>(send: any, result: T) {
send(result);
}
// I want to use this always with a type
type Num = { x: number };
sendResult<Num>(sendResponse, { x: 1 });
sendResult<Num>(sendResponse, { x: 'sss' }); // correctly showing error
// When I don't supply type, I'd like an error.
// But, T gets inferred instead of defaulting to never... so, no error :-(
sendResult(sendResponse, …
Run Code Online (Sandbox Code Playgroud) typescript ×1