标题应该是非常自我解释的.
出于调试目的,我想表达为每个服务请求打印响应代码和正文.打印响应代码很容易,但打印响应主体比较棘手,因为似乎响应主体不是作为属性提供的.
以下不起作用:
var express = require('express');
var app = express();
// define custom logging format
express.logger.format('detailed', function (token, req, res) {
return req.method + ': ' + req.path + ' -> ' + res.statusCode + ': ' + res.body + '\n';
});
// register logging middleware and use custom logging format
app.use(express.logger('detailed'));
// setup routes
app.get(..... omitted ...);
// start server
app.listen(8080);
Run Code Online (Sandbox Code Playgroud)
当然,我可以轻松地在发出请求的客户端上打印响应,但我更喜欢在服务器端进行操作.
PS:如果有帮助,我的所有回复都是json,但希望有一个解决方案适用于一般回复.
我无法理解TypeScript中术语联合类型和交集类型背后的逻辑.
从务实角度来说,如果不同类型的属性是套房产,如果我与他们结合&操作,产生的类型将成为联盟的那些套.遵循这个逻辑,我希望像这样的类型被称为联合类型.如果我将它们组合在一起|,我只能使用它们的共同属性,即集合的交集.
维基百科似乎支持这种逻辑:
任何给定非空集S的幂集(所有子集的集合)形成布尔代数,集合的代数,具有两个运算∨:=∪(并集)和∧:=∩(交集).
但是,根据typescriptlang.org,它恰恰相反:&用于生成交集类型并|用于联合类型.
我确信还有另一种方式来看待它,但我无法弄明白.
TypeScript 有没有办法将 a 转换type为 an interface?
我已经在 StackOverflow 上阅读了这个 QA,并且认为它与我在这个问题中给出的描述不太相符。
一个场景的快速示例,其中 aProduct被定义为type来自第三方 TypeScript 库。
// ProductFragmentOne is used to highlight the possibility of composition(unions, etc)
type Product = ProductFragmentOne & { sku: string };
Run Code Online (Sandbox Code Playgroud)
要将其集成Product到我们自己的系统中,可以通过扩展(联合)a来实现,type如下例所示:
export type ProductSchema = Product & {
name: string;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:
ProductSchema被定义为 an interface?这可能吗?# Example of how the code may look
export interface ProductSchema{
name: string;
//Do …Run Code Online (Sandbox Code Playgroud) I have the following Union type;
type MyUnionType = 'foo' | 'bar' | 'baz'
Run Code Online (Sandbox Code Playgroud)
I would like to create a new Union MySubUnion as a subset;
type MySubUnion = 'foo' | 'bar'
Run Code Online (Sandbox Code Playgroud)
I would like MySubUnion to be constrained to the values of its parent MyUnionType
type MySubUnion = 'foo' | 'bas' // => Error Type String 'b...
Run Code Online (Sandbox Code Playgroud)