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 Indexed Access Types.md in zh-CN #193

Open EnochGao opened 1 year ago

EnochGao commented 1 year ago

translate Indexed Access Types.md in zh-CN

github-actions[bot] commented 1 year 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 1 year ago
Translation of Indexed Access Types.md * * * title: Index access type layout: docs permalink: /zh/docs/handbook/2/indexed-access-types.html ## oneline: "Use Type['a'] syntax to get a subset of a type" We can use _Index access type_ to find another type of specific property: ```ts twoslash type Person = { age: number; name: string; alive: boolean }; type Age = Person["age"]; // ^? ``` The type being indexed is itself a type, so it is perfectly possible to use union,`keyof` or other types: ```ts twoslash type Person = { age: number; name: string; alive: boolean }; // ---cut--- type I1 = Person["age" | "name"]; // ^? type I2 = Person[keyof Person]; // ^? type AliveOrName = "alive" | "name"; type I3 = Person[AliveOrName]; // ^? ``` If you try to index a property that doesn't exist, you'll even see an error: ```ts twoslash // @errors: 2339 type Person = { age: number; name: string; alive: boolean }; // ---cut--- type I1 = Person["alve"]; ``` Another example of using an arbitrary type of index is using `number` to get the type of the array element. We can relate it to `typeof` Used together, it is convenient to get the type of array literal elements: ```ts twoslash const MyArray = [ { name: "Alice", age: 15 }, { name: "Bob", age: 23 }, { name: "Eve", age: 38 }, ]; type Person = typeof MyArray[number]; // ^? type Age = typeof MyArray[number]["age"]; // ^? // Or type Age2 = Person["age"]; // ^? ``` Indexes can only function on types above, which means they cannot be used `const` to establish a variable reference: ```ts twoslash // @errors: 2538 2749 type Person = { age: number; name: string; alive: boolean }; // ---cut--- const key = "age"; type Age = Person[key]; ``` However, you can use type aliases for similar refactoring: ```ts twoslash type Person = { age: number; name: string; alive: boolean }; // ---cut--- type key = "age"; type Age = Person[key]; ```

Generated by :no_entry_sign: dangerJS against d98e50dc28b24cf39452d3084e70c56334f080cb

EnochGao commented 1 year ago

@Kingwl 麻烦处理一下中文相关的翻译吧,我看积累好多了