Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript 3.0: unknown 类型 #5929

Merged
merged 17 commits into from
Jun 14, 2019
Merged

TypeScript 3.0: unknown 类型 #5929

merged 17 commits into from
Jun 14, 2019

Conversation

shixi-li
Copy link
Contributor

@shixi-li shixi-li commented Jun 9, 2019

译文翻译完成,resolve #5863

@lsvih lsvih changed the title Translation/typescript 3 0 the unknown type TypeScript 3.0: unknown 类型 Jun 9, 2019
@lsvih lsvih added the 前端 label Jun 9, 2019
@smilemuffie
Copy link

校对认领

@fanyijihua
Copy link
Collaborator

@smilemuffie 好的呢 🍺

@Usey95
Copy link
Contributor

Usey95 commented Jun 10, 2019

校对认领

@fanyijihua
Copy link
Collaborator

@Usey95 妥妥哒 🍻

Copy link
Contributor

@Usey95 Usey95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leviding @shixi-li 校对完成。


The main difference between `unknown` and `any` is that `unknown` is much less permissive than `any`: we have to do some form of checking before performing most operations on values of type `unknown`, whereas we don't have to do any checks before performing operations on values of type `any`.
`unknown``any` 的主要区别是 `unknown` 类型会更佳严格:在对 `unknown` 类型的值执行大多数操作之前,我们必须进行某种形式的检查。而在对 `any` 类型的值执行操作之前,我们不必进行任何检查。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"更佳严格" ==> "更加严格"


The `any` type has been in TypeScript since the first release in 2012. It represents all possible JavaScript values --- primitives, objects, arrays, functions, errors, symbols, what have you.
自从 TypeScript 2012 年发布第一个版本以来 `any` 类型就一直存在。它代表所有可能的 JavaScript 值 — 原语,对象,数组,函数,错误,符号,以及任何你可能定义的值。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原语 ==> 基本类型
Error,Symbol建议不译首字母大写


In TypeScript, every type is assignable to `any`. This makes `any` a [*top type*](https://en.wikipedia.org/wiki/Top_type) (also known as a *universal supertype*) of the type system.
TypeScript 中,每个类型都可以被定义为 `any` 类型。every type is assignable to `any`. 这让 `any` 类型成为了类型系统的 [*顶级类型*](https://en.wikipedia.org/wiki/Top_type) (也被称作 *全局超级类型*)。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

删除原文"every type is assignable to any."
==> 任何类型都可以被归为 any 类型。


Just like all types are assignable to `any`, all types are assignable to `unknown`. This makes `unknown` another top type of TypeScript's type system (the other one being `any`).
就像所有类型都可以被定义为 `any`,所有类型也都可以被定义为 `unknown`。这使得 `unknown` 成为 TypeScript 类型系统的另一种顶级类型(另一种是 `any`)。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

被定义为 ==> 被归为


Let's now look at how the `unknown` type is treated within union types. In the next section, we'll also look at intersection types.
现在让我们看一下在 union 类型中如何处理`unknown`类型。在下一节中,我们还将了解交叉类型。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

union类型 => 联合类型


```ts
type UnionType5 = unknown | any; // any
```

So why does `unknown` absorb every type (aside from `any`)? Let's think about the `unknown | string` example. This type represents all values that are assignable to type `unknown` plus those that are assignable to type `string`. As we've learned before, all types are assignable to `unknown`. This includes all strings, and therefore, `unknown | string` represents the same set of values as `unknown` itself. Hence, the compiler can simplify the union type to `unknown`.
所以为什么 `unknown` 可以吸收任何类型(`any` 类型除外)?让我们来想想 `unknown | string` 这个例子。这个类型表示可以被定义为 `unknown` 类型的所有的值加上可以被定义成 `string` 类型的所有的值。就像我们之前了解到的,所有类型的值都可以被定义为 `unknown` 类型,其中也包括了所有的 `string` 类型,也就是说因此,`unknown | string` 就是表示和 `unknown` 类型本身相同的值集。因此,编译器可以将联合类型简化为 `unknown` 类型。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『这个类型可以表示任何 unkown 类型或者 string 类型的值』
『也就是说因此』=> 『因此』

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同意校对的说法,更加简洁


- `===`
- `==`
- `!==`
- `!=`

If you want to use any other operators on a value typed as `unknown`, you have to narrow the type first (or force the compiler to trust you using a type assertion).
如果要对类型为 `unknown` 的值使用任何其他运算符,则必须先指定类型(或强制编译器使用类型断言信任您)。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『或强制编译器使用类型断言信任您』 => 『或使用类型断言强制编译器信任你』


Let's assume we want to write a function that reads a value from `localStorage`and deserializes it as JSON. If the item doesn't exist or isn't valid JSON, the function should return an error result; otherwise, it should deserialize and return the value.
假设我们要编写一个从 `localStorage` 读取值并将其反序列化为 JSON 的函数。如果该项不存在或者是无效 JSON,则该函数应返回错误结果;否则,它应该反序列化并返回值。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

分号改逗号


1. The value `null` is a valid JSON value. Therefore, we would not be able to distinguish whether we deserialized the value `null` or whether the entire operation failed because of a missing item or a syntax error.
2. If we were to return `null` from the function, we could not return the error at the same time. Therefore, callers of our function would not know why the operation failed.
1. `null` 值是一个无效的 JSON 值。因此,我们无法区分是对值 `null` 进行了反序列化,还是由于缺少参数或语法错误而导致整个操作失败。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『无效』=> 『有效』


For the sake of completeness, a more sophisticated alternative to this approach is to use [typed decoders](https://dev.to/joanllenas/decoding-json-with-typescript-1jjc) for safe JSON parsing. A decoder lets us specify the expected schema of the value we want to deserialize. If the persisted JSON turns out not to match that schema, the decoding will fail in a well-defined manner. That way, our function always returns either a valid or a failed decoding result and we could eliminate the `unknown` type altogether.
为了返回完整性,使用这种方法的更复杂的替代方案[类型解码器](https://dev.to/joanllenas/decoding-json-with-typescript-1jjc) 来用于安全的 JSON 解析。解码器需要我们指定要反序列化的值的预期数据结构。如果持久化的JSON结果与该数据结构不匹配,则解码将以明确定义的方式失败。这样,我们的函数总是返回有效或失败的解码结果,我们可以完全消除`unknown`类型。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『为了返回完整性,使用这种方法的更复杂的替代方案』=> 『为了完整性,这种方法的梗成熟的替代方案是使用类型解码器进行安全的 JSON 解析。』
『我们可以完全消除unknown类型』 => 『就不再需要 unknown 类型了。』

@leviding leviding added the enhancement 等待译者修改 label Jun 11, 2019
Copy link

@smilemuffie smilemuffie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

译者可以在第一位校对者的建议上改一下哦,简洁化下一些语句,翻译的很棒,校对的也很棒

@@ -1,27 +1,27 @@
> * 原文地址:[TypeScript 3.0: The unknown Type](https://mariusschulz.com/blog/typescript-3-0-the-unknown-type)
> * 原文地址:[TypeScript 3.0: The unknown Type](https://mariusschulz.com/blog/typescript-3-0-the-unknown-type)
> * 原文作者:[Marius Schulz](https://mariusschulz.com)
> * 译文出自:[掘金翻译计划](https://github.com/xitu/gold-miner)
> * 本文永久链接:[https://github.com/xitu/gold-miner/blob/master/TODO1/typescript-3-0-the-unknown-type.md](https://github.com/xitu/gold-miner/blob/master/TODO1/typescript-3-0-the-unknown-type.md)
> * 译者:
> * 校对者

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

译者,校对者的 github 链接都漏了哦


```ts
type UnionType5 = unknown | any; // any
```

So why does `unknown` absorb every type (aside from `any`)? Let's think about the `unknown | string` example. This type represents all values that are assignable to type `unknown` plus those that are assignable to type `string`. As we've learned before, all types are assignable to `unknown`. This includes all strings, and therefore, `unknown | string` represents the same set of values as `unknown` itself. Hence, the compiler can simplify the union type to `unknown`.
所以为什么 `unknown` 可以吸收任何类型(`any` 类型除外)?让我们来想想 `unknown | string` 这个例子。这个类型表示可以被定义为 `unknown` 类型的所有的值加上可以被定义成 `string` 类型的所有的值。就像我们之前了解到的,所有类型的值都可以被定义为 `unknown` 类型,其中也包括了所有的 `string` 类型,也就是说因此,`unknown | string` 就是表示和 `unknown` 类型本身相同的值集。因此,编译器可以将联合类型简化为 `unknown` 类型。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同意校对的说法,更加简洁

@smilemuffie
Copy link

校对完毕 @shixi-li @leviding @Usey95 翻译者校对者记得添加github链接信息哦

@leviding
Copy link
Member

@shixi-li 有空的时候可以修改啦

@shixi-li
Copy link
Contributor Author

@Usey95 @smilemuffie 非常感谢两位的校正。

@fanyijihua @leviding 修改完毕。

Copy link
Member

@leviding leviding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

各位辛苦啦,译者 @shixi-li 看下最新的一个 commit,我修改了一些细节问题,麻烦注意一下 ⚠️

@leviding leviding merged commit 1d79ecf into xitu:master Jun 14, 2019
@leviding
Copy link
Member

@shixi-li 已经 merge 啦~ 快快麻溜发布到掘金然后给我发下链接,方便及时添加积分哟。

掘金翻译计划有自己的知乎专栏,你也可以投稿哈,推荐使用一个好用的插件
专栏地址:https://zhuanlan.zhihu.com/juejinfanyi

@leviding leviding added 翻译完成 and removed enhancement 等待译者修改 labels Jun 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeScript 3.0: unknown 类型
7 participants