Skip to content

Revised some awkward expression#136

Closed
MilkClouds wants to merge 1 commit intomicrosoft:mainfrom
MilkClouds:patch-1
Closed

Revised some awkward expression#136
MilkClouds wants to merge 1 commit intomicrosoft:mainfrom
MilkClouds:patch-1

Conversation

@MilkClouds
Copy link

Revised some awkward expression.

Revised some awkward expression.
@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2022

Thanks for the PR!

This section of the codebase is owned by @bumkeyy, @yeonjuan, @guyeol, and @dvlprsh - if they write a comment saying "LGTM" then it will be merged.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2022

Translation of TS for OOPers.md

title: TypeScript for Java/C# Programmers
short: TS for Java/C# Programmers
layout: docs
permalink: /ko/docs/handbook/typescript-in-5-minutes-oop.html

oneline: Learn TypeScript if you have a background in object-oriented languages

TypeScript is a popular choice for programmers who are familiar with languages that use static typing, such as C#, Java, and so on.

TypeScript's type system offers many benefits of static typing, such as better code completion, early detection of errors, and clearer communication between parts of the program.
TypeScript provides a lot of familiar functionality for these developers, but we need to revisit how JavaScript (as does TypeScript) differs from traditional object-oriented programming (OOP) languages.
Understanding these differences will help you write better JavaScript code and avoid the common pitfalls faced by programmers who have entered TypeScript directly from C#/Java.

Learn JavaScript Together (Co-learning JavaScript)

If you're already familiar with JavaScript but are primarily programmers using Java or C#, this introductory page can help explain common misconceptions and pitfalls.
Some of the ways typescript models are quite different from Java or C#, and it's important to keep this in mind when learning TypeScript.

If you're a Java or C# programmer new to JavaScript, you can first understand the runtime behavior of JavaScript. Except It's a good idea to learn part of JavaScript.
TypeScript uses code running Since it doesn't change the way it works, you still need to learn how JavaScript works in order to write code that actually works!

TypeScript is the same as JavaScript _Runtime_It is very important to remember that resources that want to implement specific runtime behaviors (such as converting strings to numbers, displaying warnings, writing files to disk, etc.) always apply equally well to TypeScript programs.
Don't limit yourself to resources specific to TypeScript!

Rethinking the Class

C# and Java Mandatory OOP It's called language.
In these languages, _class_Is not only the default unit of code configuration, but all data at runtime. and A basic container of actions.
Forcing all functionality and data into a class can be a good domain model for some problems, but all domains will be represented in this way. _need_There is no .

Free functions and data

In JavaScript, functions can be anywhere and can freely pass data without belonging to a predefined 'class' or 'struct'.
This flexibility is very powerful.
"Free" (not associated with a class) function that processes data regardless of the OOP layer is preferred as a model for writing programs to JavaScript.

Static Classes

In addition, certain structures, such as singletons and static classes in C# and Java, are not required by TypeScript.

OOP in TypeScript (OOP in TypeScript) in TypeScript

In other words, you can continue using the class if you want!
Some problems are appropriate to solve with existing OOP layers, and TypeScript supports classes in JavaScript, making these models more effective.
TypeScript supports many common patterns, such as interfaces, inheritance, and static method implementations.

We will cover the class later in this guide.

Rethinking Types

TypeScript's _type_The understanding of is actually quite different from C# or Java.
Let's look at some of the differences.

Type system embodied by name (Terminal Reified Type Systems)

The values and objects given in C# and Java have exactly one type: 'null', the raw type, or the defined class type.
To ask for the correct type at runtime value.GetType() or value.getClass()You can call a method such as .
The definition of this type exists somewhere in the class with a specific name, and cannot be replaced by each other, even if the two classes have similar forms unless they have explicit inheritance relationships or commonly implemented interfaces.

This is what reified, nominal Describes the type system.
The type used in the code exists at runtime, and the type is associated with a declaration rather than a structure.

Type as a set (Types as Sets)

In C# or Java, the one-to-one correspondence between the runtime type and its compile-time declaration is important.

In TypeScript, the type shares something common _Set of values_It's a good idea to think of it as .
Because the type is only a set, a specific value is numerous It can belong to a set.

Once you start thinking about types as a set, certain operations become very natural.
For example, in C#, 'string' and 'int' both possible It is strange to pass this value as an abbreviation because the type does not exist.

In TypeScript, this becomes very natural when you realize that every type is simply a set.
How do you describe the values that can belong to a 'string' or 'number' set?
This value is simply Union: ‘string | Belongs to number'.

TypeScript provides several ways to use types based on collective theory, and it is more intuitive to think of types as a set.

Deleted Structural Types

In TypeScript, an object has exactly a single type. No.
For example, when you create an object that satisfies the interface, you can use it where the interface is expected, even if there is no declarative relationship between the two.

interface Pointlike {
  x: number;
  y: number;
}
interface Named {
  name: string;
}

function printPoint(point: Pointlike) {
  console.log("x = " + point.x + ", y = " + point.y);
}

function printName(x: Named) {
  console.log("Hello, " + x.name);
}

const obj = {
  x: 0,
  y: 0,
  name: "Origin",
};

printPoint(obj);
printName(obj);

TypeScript's type system is not _Structured_Is: objis a number. xand y As I have a property, Pointlikecan be used as .
The relationship between types is determined by the properties they contain, not by whether they are declared as specific relationships.

TypeScript's type system also Not materialized: At runtime objprice PointlikeI do not tell you that it is.
In fact Pointlike The type is in any form It does not exist.

Type as a set In a concept, obj Pointlike A set of values or Named You can consider it a member of a set of values.

Results of structural typeization (Concequencys of Structural Typing)

Object-oriented programmers are often surprised by two aspects of structural typeization.

Empty Type (Empty Types)

First _Empty type_seems to defy expectations:

class Empty {}

function fn(arg: Empty) {
  // 무엇인가를 하나요?
}

// 오류는 없지만, '빈' 타입은 아니지 않나요?
fn({ k: 10 });

TypeScript allows a given argument to be EmptyCheck if it's fnCheck that the call in is valid
{ k: 10 }and class Empty { }of _Check the structure to validate it.
EmptyThere are no properties in . Emptyto perform _All_ Properties { k: 10 }it belongs to .
Therefore, this is a valid call!

This may seem surprising, but it has a very similar relationship to what is finally implemented in a nominal object-oriented programming language.
Because the natural subtype relationship between the subclass and its base class is destroyed, the subclass has the property of the base class. _delete_I can't.
Structural type systems implicitly distinguish these relationships by simply depicting subtypes with compatible types of properties.

Same Type (Identical Types)

The causes of another frequent surprise are caused by the same type:

class Car {
  drive() {
    // hit the gas
  }
}
class Golfer {
  drive() {
    // hit the ball far
  }
}

// No error?
let w: Car = new Golfer();

Again, the reason why it's not an error is _rescue_because it is the same.
This may be a potential reason for confusion, but it is not common when classes that don't matter are the same.

You'll learn more about how classes relate to each other in class chapters in the future.

Reflection

Object-oriented programmers are accustomed to being able to query any type of value, including generics.

// C#
static void PrintType<T>() {
    Console.WriteLine(typeof(T).Name);
}

Because TypeScript's type system is completely wiped out, information such as instantiation of generic type factors is not available at runtime.

JavaScript typeofand instanceofThere are limited primitives, such as , but you should know that these operators still work because they exist in the output of the code that has been cleared of the type.
For example typeof (new Car()) CarI "Car"non- "object"Is.


It's been an overview so far, and here we are. handbookRead or Playground exampleExplore .

Generated by 🚫 dangerJS against 0aad79e

@MilkClouds MilkClouds changed the title Update TS for OOPers.md Revised some awkward expression Feb 4, 2022
Copy link
Contributor

@yeonjuan yeonjuan left a comment

Choose a reason for hiding this comment

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

@MilkClouds 감사합니다! 몇 가지 제안남겼습니다.

@MilkClouds MilkClouds closed this by deleting the head repository Feb 14, 2025
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.

2 participants