Skip to content

feat: translate Keyof Type Operator.md in zh-CN #176

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

EnochGao
Copy link

translate Keyof Type Operator.md in zh-CN

@github-actions
Copy link
Contributor

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
Copy link
Contributor

Translation of Keyof Type Operator.md

title: The Keyof type operator
layout: docs
permalink: /zh/docs/handbook/2/keyof-types.html

oneline: "Using the keyof operator in a type context"

keyof Type operator

keyof An operator accepts an object type and combines the key values of the object to produce a literal string consisting of strings or numbers.
The following type P is | with type "x" "y" is equivalent:

type Point = { x: number; y: number };
type P = keyof Point;
//   ^?

If the type hasstringornumberindex signature,keyofThe type of index will be returned:

type Arrayish = { [n: number]: unknown };
type A = keyof Arrayish;
//   ^?

type Mapish = { [k: string]: boolean };
type M = keyof Mapish;
//   ^?

It is worth noting that in the above example, M be string | number -- This is because the keys of objects in JavaScript are always cast to strings, so obj[0] and obj["0"]is equivalent.

whilekeyofThis becomes especially useful when combined with mapping types, as we'll learn more about later.

Generated by 🚫 dangerJS against 8376e15

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.

1 participant