Translate 1 file to ko, playground - Structural Typing#87
Translate 1 file to ko, playground - Structural Typing#87yahma25 wants to merge 115 commits intomicrosoft:mainfrom
Conversation
The word" Community" is commonly translated to "社区".
It reads cleaner with other nav items.
playground-Discriminate Types
Co-authored-by: Kibeom Kwon <kgbum2222@gmail.com>
Co-authored-by: Kibeom Kwon <kgbum2222@gmail.com>
Co-authored-by: Kibeom Kwon <kgbum2222@gmail.com>
Co-authored-by: Kibeom Kwon <kgbum2222@gmail.com>
Co-authored-by: Kibeom Kwon <kgbum2222@gmail.com>
Co-authored-by: Kibeom Kwon <kgbum2222@gmail.com>
Co-authored-by: Kibeom Kwon <kgbum2222@gmail.com>
Co-authored-by: Kibeom Kwon <kgbum2222@gmail.com>
Co-authored-by: Kibeom Kwon <kgbum2222@gmail.com>
Co-authored-by: Kibeom Kwon <kgbum2222@gmail.com>
Co-authored-by: Kibeom Kwon <kgbum2222@gmail.com>
…ound-Indexed-Types Translate 1 file to ko. playground Indexed Types
…Support Translate 1 file to ko - Mobile Support
Co-authored-by: YeonJuan <yeonjuan93@naver.com>
…ound-Discriminate-Types Translate 1 file to ko. playground-Discriminate Types
…translation [PT-BR] Translate file Introduction
Fix typo in 'opeartor' to 'operator'
Fix typo in JSX.md
…nd-3-7-syntax-and-messaging Translate FR: playground/fr/3-7/Syntax and messaging
…nd-3-7-syntax-and-messaging-typo-fix FR translation: fix typo
…ng.ts Co-authored-by: YeonJuan <yeonjuan93@naver.com>
…Localizations into Translation-to-ko-playground-Conditional-Types
…ound-Nominal-Typing Translate 1 file to ko, playground - Nominal Typing
…ound-Conditional-Types Translate 1 file to ko playground - Conditional Types
…tructural-Typing' into Translation-to-ko-palyground-Structural-Typing
Translation of noResolve.mddisplay: "No Resolve" oneline: "Disallow
|
| Mode | Input | Output | Extension Output file |
|---|---|---|---|
preserve |
<div /> |
<div /> |
.jsx |
react |
<div /> |
React.createElement("div") |
.js |
react-native |
<div /> |
<div /> |
.js |
You can set this mode using flag command line --jsx or the appropriate options in the file tsconfig.json You.
*Note: You can specify a function factory JSX to be used when targeting react JSX issuer with options
--jsxFactory(default value toReact.createElement)
Operator as
Remember how to write type affirmations:
var foo = <foo>bar;This confirms the variable bar have type foo.
Since TypeScript also uses square brackets for type affirmation, combining it with the JSX syntax will cause certain difficulties. As a result, TypeScript does not allow the use of square brackets for type affirmations on files. .tsx.
Because the syntax above cannot be used on files .tsx, then an alternative to type affirmation can use the operator as.
Examples can be easily rewritten with the operator. as.
var foo = bar as foo;Operator as available in both file types, .ts and .tsx, and has the same treatment as type affirmation using elbow brackets.
Type Check
The order to be understood regarding type checking in JSX, i.e. you must first understand the difference between intrinsic elements and value-based elements. There's an expression. <expr /> and expr which may refer to something intrinsic to an environment (e.g. div or span in a DOM environment) or on custom components that you have created.
This is important for the following two reasons:
- For React, the intrinsic element is considered a string (
React.createElement("div")The components you make are not (React.createElement(MyComponent)). - The type of attribute passed to the JSX element should look different.
Intrinsic element attributes should be known Intrinsically while the components will like to want to determine their own set of attributes.
TypeScript uses Some conventions with React to distinguish it.
Intrinsic elements always start with lowercase, and value-based elements always start with uppercase.
Intrinsic element
Intrinsic elements are sought in Interface Specifically, that is JSX.IntrinsicElements.
The standard, if Interface This is not specified, so whatever happens and the intrinsic element will not be checked for type.
However, if Interface If this exists, the name of the intrinsic element will be searched as a property in Interface JSX.IntrinsicElements.
Examples:
declare namespace JSX {
interface IntrinsicElements {
foo: any;
}
}
<foo />; // ok
<bar />; // galatIn the example above, <foo /> It's going to go well, but <bar /> it will cause an error, because <bar /> not specified on Interface JSX.IntrinsicElements.
Note: You can also specify indexer to get all string-type elements inside
JSX.IntrinsicElements, as follows:
declare namespace JSX {
interface IntrinsicElements {
[elemName: string]: any;
}
}Value-Based Elements
Value-based elements will be searched by Identifier Which is in one Scope.
import MyComponent from "./myComponent";
<MyComponent />; // ok
<SomeOtherComponent />; // galatThere are two ways to define a value-based element:
- Function Component (FC)
- Class Component
Since these two types of value-based elements are indistinguishable from each other in JSX expressions, then first TS will try to complete the expression as Function Component use Overloading. If the process is successful, then TS completes the expression to its declaration. If it fails to complete as Function Component, then TS will then try to solve it as Class Component. If it fails, TS will report an error.
Function Component
As the name suggests, this component is defined using the JavaScript function where the first argument is a props object.
TS insists that the type of return must be able to be given to JSX.Element.
interface FooProp {
name: string;
X: number;
Y: number;
}
declare function AnotherComponent(prop: {name: string});
function ComponentFoo(prop: FooProp) {
return <AnotherComponent name={prop.name} />;
}
const Button = (prop: {value: string}, context: { color: string }) => <button>Because Function Component Is a JavaScript function, so the overload function can be used here:
interface ClickableProps {
children: JSX.Element[] | JSX.Element
}
interface HomeProps extends ClickableProps {
home: JSX.Element;
}
interface SideProps extends ClickableProps {
side: JSX.Element | string;
}
function MainButton(prop: HomeProps): JSX.Element;
function MainButton(prop: SideProps): JSX.Element {
...
}Note: Function Component Previously known as Stateless Function Component (SFC). Because Function Component It can no longer be considered Stateless in the latest version of react,
SFCand his aliasStatelessComponentNot used anymore.
Class Component
This makes it possible to define the type of class component.
However, to do so, it is best to understand the following two new terms: element type class and element type instance.
If there is <Expr />so type class elements be Expr.
In the example above, if MyComponent be class ES6, then type class-He is a constructor and static from class aforementioned.
If MyComponent It's a factory function, and it's a type of class-It's a function in itself.
After the class type is created, the type instance determined by the combination of return types from type construction class or call signature (Wherever there is).
So again, in the case of the ES6 class, the instance type is that type of class instance, and in the case of factory function, it will be the type of value returned from the function.
class MyComponent {
render() {}
}
// menggunakan konstruksi signature
var myComponent = new MyComponent();
// tipe class elemen => MyComponent
// tipe instance elemen => { render: () => void }
function MyFactoryFunction() {
return {
render: () => {},
};
}
// menggunakan call signature
var myComponent = MyFactoryFunction();
// tipe class elemen => FactoryFunction
// tipe instance elemen => { render: () => void }Element type instance That's interesting, because it has to be-assign to JSX.ElementClass Or the result will be an error.
Default JSX.ElementClass be {}, but this can be added to limit the use of JSX only to the type that suits the right interface.
declare namespace JSX {
interface ElementClass {
render: any;
}
}
class MyComponent {
render() {}
}
function MyFactoryFunction() {
return { render: () => {} };
}
<MyComponent />; // ok
<MyFactoryFunction />; // ok
class NotAValidComponent {}
function NotAValidFactoryFunction() {
return {};
}
<NotAValidComponent />; // galat
<NotAValidFactoryFunction />; // galatAttribute Type Check
The first step to checking an attribute type is to specify the type of element attribute.
It differs slightly between intrinsic and value-based elements.
For intrinsic elements, this is a type of property on JSX.IntrinsicElements
declare namespace JSX {
interface IntrinsicElements {
foo: { bar?: boolean };
}
}
// tipe atribut elemen untuk 'foo' is '{bar?: boolean}'
<foo bar />;For value-based elements, this is a little more complex.
This is determined by the type of property on instance element type which has been determined.
Which properties are used to be determined by JSX.ElementAttributesProperty.
It must be declared with one property.
The name of the property was later used.
Start TypeScript 2.8, if JSX.ElementAttributesProperty not provided, the first type of parameter of the constructor of class elements or summoning Function Component It will be used instead.
declare namespace JSX {
interface ElementAttributesProperty {
props; // menentukan nama properti yang akan digunakan
}
}
class MyComponent {
// menentukan properti pada tipe instance elemen
props: {
foo?: string;
};
}
// tipe atribut elemen untuk 'MyComponent' adalah '{foo?: string}'
<MyComponent foo="bar" />;Element attribute types are used to check attributes in JSX.
Optional and compulsory properties are supported.
declare namespace JSX {
interface IntrinsicElements {
foo: { requiredProp: string; optionalProp?: number };
}
}
<foo requiredProp="bar" />; // ok
<foo requiredProp="bar" optionalProp={0} />; // ok
<foo />; // galat, tidak ada requiredProp
<foo requiredProp={0} />; // galat, requiredProp seharusnya bertipe string
<foo requiredProp="bar" unknownProp />; // galat, unknownProp tidak ada
<foo requiredProp="bar" some-unknown-prop />; // ok, karena 'some-unknown-prop' bukan identifier yang validNote: If the attribute name is not Identifier Valid JS (such as attributes
data- *), it is not considered an error if it is not found in the element attribute type.
In addition, the interface JSX.IntrinsicAttributes can be used to determine additional properties used by Framework JSX is not generally used by props or component arguments – for example key in React. Specializing further, generic types JSX.IntrinsicClassAttributes<T> It can also be used to assign the same additional attribute type only to class component (And not function component). In this type, generic parameters correspond to the type instance class. In React, this is used to allow attributes ref with type Ref <T>. In general, all properties on this interface must be optional, unless you intend to have users Framework JSX you need to provide multiple attributes on each tag.
Operator spread It also works:
var props = { requiredProp: "bar" };
<foo {...props} />; // ok
var badProps = {};
<foo {...badProps} />; // galatExamination Children Type
In TypeScript 2.3, TS introduces type checks to children. Children It is a special property in element attribute type where child JSXExpression taken to be incorporated into the attribute.
Similar to how to use JSX.ElementAttributesProperty To determine the name of Props, TS uses JSX.
JSX.ElementChildrenAttribute must be declared with a single property.
declare namespace JSX {
interface ElementChildrenAttribute {
children: {}; // menentukan nama children untuk digunakan
}
}<div>
<h1>Hello</h1>
</div>;
<div>
<h1>Hello</h1>
World
</div>;
const CustomComp = (props) => <div>{props.children}</div>
<CustomComp>
<div>Hello World</div>
{"This is just a JS expression..." + 1000}
</CustomComp>You can determine the type of children Like any other attribute. This will change the standard type, for example. React typings If you use it.
interface PropsType {
children: JSX.Element
name: string
}
class Component extends React.Component<PropsType, {}> {
render() {
return (
<h2>
{this.props.children}
</h2>
)
}
}
// OK
<Component name="foo">
<h1>Hello World</h1>
</Component>
// Error: children adalah tipe JSX.Element, bukan array dari JSX.Element.
<Component name="bar">
<h1>Hello World</h1>
<h2>Hello World</h2>
</Component>
// Error: children adalah tipe JSX.Element, bukan array dari JSX.Element maupun string.
<Component name="baz">
<h1>Hello</h1>
World
</Component>JSX result type
By default, the result of JSX expression is of the type any.
You can adjust the type by specifying Interface JSX.Element.
However, it is not possible to retrieve type information about elements, attributes or derivatives of JSX from Interface this.
That is black box.
Embed Expressions
JSX allows you to embed expressions between tags by flanking expressions with curly brackets ({}).
var a = (
<div>
{["foo", "bar"].map((i) => (
<span>{i / 2}</span>
))}
</div>
);The code above will result in an error because you can't split String with numbers.
Output- when using the option preserve, it looks like:
var a = (
<div>
{["foo", "bar"].map(function (i) {
return <span>{i / 2}</span>;
})}
</div>
);React integration
To use JSX with React, you must use React typings. These typings define namespace JSX It's appropriate to use with React.
/// <reference path="react.d.ts" />
interface Props {
foo: string;
}
class MyComponent extends React.Component<Props, {}> {
render() {
return <span>{this.props.foo}</span>;
}
}
<MyComponent foo="bar" />; // ok
<MyComponent foo={0} />; // galatFactory Function
Factory function used by compiler options jsx: react configurable. It can be set using command line options jsxFactory, or comments @jsx line to set it on a per file basis. For example, if you set jsxFactory to createElement, <div /> Will do it as createElement("div") not React.createElement("div").
The preview version of the comment can be used as such (in TypeScript 2.8):
import preact = require("preact");
/* @jsx preact.h */
const x = <div />;It will be:
const preact = require("preact");
const x = preact.h("div", null);Factory The selected will also affect where namespace JSX searched (for type check information) before returning to Global. If factory specified as React.createElement (standard), the compiler will check React.JSX Before checking JSX Global. If factory specified as h, then he'll check. h.JSX before JSX Global.
|
🤔 action build 중 실패한 게 있어서 origin/main 으로 병합하여 최신화 했더니 이 이슈를 초과하는 변경사항까지 반영되었네요. 다시 올리겠습니다. |
en
번역 리뷰 요청합니다: @yeonjuan @bumkeyy
ref #6
감사합니다 😄