相关疑难解决方法(0)

表达记录响应机构

标题应该是非常自我解释的.

出于调试目的,我想表达为每个服务请求打印响应代码和正文.打印响应代码很容易,但打印响应主体比较棘手,因为似乎响应主体不是作为属性提供的.

以下不起作用:

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,但希望有一个解决方案适用于一般回复.

node.js express

51
推荐指数
10
解决办法
4万
查看次数

TypeScript的并集和交集类型的命名

我无法理解TypeScript中术语联合类型交集类型背后的逻辑.

从务实角度来说,如果不同类型的属性是套房产,如果我与他们结合&操作,产生的类型将成为联盟的那些套.遵循这个逻辑,我希望像这样的类型被称为联合类型.如果我将它们组合在一起|,我只能使用它们的共同属性,即集合的交集.

维基百科似乎支持这种逻辑:

任何给定非空集S的幂集(所有子集的集合)形成布尔代数,集合的代数,具有两个运算∨:=∪(并集)和∧:=∩(交集).

但是,根据typescriptlang.org,它恰恰相反:&用于生成交集类型|用于联合类型.

我确信还有另一种方式来看待它,但我无法弄明白.

boolean-logic set typescript

35
推荐指数
4
解决办法
1430
查看次数

如何在 Typescript 中将类型转换为接口

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)

我的问题是:

  • 有没有办法让 ourProductSchema被定义为 an interface?这可能吗?
# Example of how the code may look
export interface ProductSchema{ 
  name: string;
  //Do …
Run Code Online (Sandbox Code Playgroud)

types interface typescript

9
推荐指数
1
解决办法
2万
查看次数

Typescript extract and create Union as a subset of a Union

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)

typescript

4
推荐指数
2
解决办法
397
查看次数

标签 统计

typescript ×3

boolean-logic ×1

express ×1

interface ×1

node.js ×1

set ×1

types ×1