microsoft / TypeScript-Website-Localizations

A repo for the TypeScript Website localizations
Creative Commons Attribution 4.0 International
118 stars 132 forks source link

feat: translate Typeof Type Operator.md in zh-CN #180

Open EnochGao opened 2 years ago

EnochGao commented 2 years ago

translate Typeof Type Operator.md in zh-CN

github-actions[bot] commented 2 years ago

Thanks for the PR!

This section of the codebase is owned by @Kingwl - if they write a comment saying "LGTM" then it will be merged.

github-actions[bot] commented 2 years ago
Translation of Typeof Type Operator.md * * * title: The Typeof type operator layout: docs permalink: /zh/docs/handbook/2/typeof-types.html oneline: "Use the typeof type operator in the context of type." ## translatable: true ## `typeof` Type operator JavaScript already has one `typeof` operator, you can in _expression_ Use in context: ```ts twoslash // 打印出 "string" console.log(typeof "Hello world"); ``` TypeScript added one `typeof` operator that allows you to use it in _type_ It is used in context to reference variables or properties _type_: ```ts twoslash let s = "hello"; let n: typeof s; // ^? ``` This is not very useful for primitive types, but in combination with other type operators, you can use it `typeof` Convenient representation of multiple forms. For example, let's start with a predefined type `ReturnType` Start viewing. It accepts one _Function type_ And generate the return type of the function: ```ts twoslash type Predicate = (x: unknown) => boolean; type K = ReturnType; // ^? ``` If we try to use on the function name `ReturnType` , we will see a guiding error: ```ts twoslash // @errors: 2749 function f() { return { x: 10, y: 3 }; } type P = ReturnType; ``` Remember,_values_ and _types_ Not the same thing. To reference the function `f` The type of value we use `typeof`: ```ts twoslash function f() { return { x: 10, y: 3 }; } type P = ReturnType; // ^? ``` ### limit TypeScript intentionally limits its usability `typeof` The kind of expression. Specifically, only on identifiers (i.e. variable names) or their properties `typeof` is legal. This helps avoid confusing traps of writing code that you think is executing but isn't: ```ts twoslash // @errors: 1005 declare const msgbox: () => boolean; // type msgbox = any; // ---cut--- // 以为等同于 ReturnType,实际是错误的 let shouldContinue: typeof msgbox("Are you sure you want to continue?"); ```

Generated by :no_entry_sign: dangerJS against 2e48ba0ddbd83d8e0262ba04d270013fd9e1f72e