[英]Return same object as argument
This is probably an anti-pattern, but I want to return the same object as an argument, in this case like so: 这可能是一种反模式,但我想返回与参数相同的对象,在这种情况下,如下所示:
const handleConnection = (s: net.Socket): net.Socket => {
s.pipe(createParser()).on('data', (d: any) => {
log.info(chalk.green.underline('dygrep server response:'));
if (d && d.lastMessage) {
process.stdout.write(prompt);
}
});
return s;
};
so what would be ideal is to do something like this: 所以理想的是做这样的事情:
const handleConnection = (s: net.Socket): s => {
s.pipe(createParser()).on('data', (d: any) => {
log.info(chalk.green.underline('dygrep server response:'));
if (d && d.lastMessage) {
process.stdout.write(prompt);
}
});
return s;
};
but yeah that's not quite right - how do I tell TypeScript that I am returning one of the arguments? 但是是的,这不太正确-如何告诉TypeScript我要返回其中一个参数?
how do I tell TypeScript that I am returning one of the arguments
我如何告诉TypeScript我正在返回参数之一
Generics. 泛型。 The constraint is the return is same as argument eg
约束是返回与参数相同,例如
function handleConnection<T extends net.Socket>(arg:T):T{}
Here whatever is passed in as arg is what is returned. 在这里,作为arg传递的是返回的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.