在Typescript上声明类型时,领先管道的建议是什么 [英] What's the propose of a leading pipe when declaring a type on Typescript

查看:74
本文介绍了在Typescript上声明类型时,领先管道的建议是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Prettiefier,我注意到此代码块的格式设置为包含额外的前导管道,请参见以下示例:

Using Prettiefier I noticed that this code block is formated to contain an extra leading pipe, see the following example:

// before Prettier
let foo: {
    [k: string]: any
} | boolean;

// after Prettier
const actions:
  | {
      [k: string]: any;
    }
  | boolean = true;

请注意Prettier在类型声明中添加的管道.

Notice the pipe added by Prettier on the type declaration.

这也可以在一行中声明,并且更漂亮地保留格式而不添加额外的管道:

This could also be declared in a single line, and prettier keeps the format without adding the extra pipe:

const actions: { [k: string]: any } | boolean = true;

我的疑问是为什么要添加此管道?它在Typescript级别上有什么改变吗?

My doubt is why is this pipe added? Does it change anything at the Typescript level?

推荐答案

它纯粹是风格上的,没有功能上的差异.

It's purely stylistic, there is no functional difference.

请考虑以下内容:

type Foo = Bar
  | Baz
  | Bap

相比:

type Foo =
  | Bar
  | Baz
  | Bap

第二个示例更加简洁,并且很明显, | s右侧的三件事是联合的组成部分.

The second example is a lot cleaner, and it's immediately clear that the three things on the right side of the |s are the constituents of the union.

很明显,在一行中定义所有内容时,您不会添加前导 | :

Clearly, you wouldn't add a leading | when defining everything on one line:

type T = A | B

这篇关于在Typescript上声明类型时,领先管道的建议是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆