-
Notifications
You must be signed in to change notification settings - Fork 130
[ja translation] docs/tutorials/* #25
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
base: main
Are you sure you want to change the base?
Conversation
Thanks for the PR! This section of the codebase is owned by @sasurau4, @Quramy, and @Naturalclar - if they write a comment saying "LGTM" then it will be merged. |
Translation of TypeScript Tooling in 5 minutes.mdtitle: 5-minute TypeScript tool translatable: trueLet's start by creating a simple web application using TypeScript. Install TypeScriptThere are two main ways to make TypeScript available in your project:
Visual Studio 2017 and Visual Studio 2015 Update 3 include TypeScript by default. If you want to use npm, click here: > npm install -g typescript Create your first TypeScript fileIn the editor // @noImplicitAny: false
function greeter(person) {
return "Hello, " + person;
}
let user = "Jane User";
document.body.textContent = greeter(user); Compiling codeAbove On the command line, run the TypeScript compiler: tsc greeter.ts The result included the same as the JavaScript I just entered. Now you can take advantage of the new tools that TypeScript offers. function greeter(person: string) {
return "Hello, " + person;
}
let user = "Jane User";
document.body.textContent = greeter(user); Type annotationsTypeScript type annotations are a lightweight way to record intentional constraints on functions and variables. // @errors: 2345
function greeter(person: string) {
return "Hello, " + person;
}
let user = [0, 1, 2];
document.body.textContent = greeter(user); When I recompile, I get an error: error TS2345: Argument of type 'number[]' is not assignable to parameter of type 'string'. Similarly, try removing all arguments you pass to the greeter function call. Despite the error interfaceLet's further develop the sample. This section uses an interface that describes objects with firstName and lastName fields. interface Person {
firstName: string;
lastName: string;
}
function greeter(person: Person) {
return "Hello, " + person.firstName + " " + person.lastName;
}
let user = { firstName: "Jane", lastName: "User" };
document.body.textContent = greeter(user); classFinally, let's extend this example only once again using a class. Here we have a constructor and some public fields In addition, the constructor's arguments class Student {
fullName: string;
constructor(
public firstName: string,
public middleInitial: string,
public lastName: string
) {
this.fullName = firstName + " " + middleInitial + " " + lastName;
}
}
interface Person {
firstName: string;
lastName: string;
}
function greeter(person: Person) {
return "Hello, " + person.firstName + " " + person.lastName;
}
let user = new Student("Jane", "M.", "User");
document.body.textContent = greeter(user);
Run typescript web apps
<!DOCTYPE html>
<html>
<head>
<title>TypeScript Greeter</title>
</head>
<body>
<script src="greeter.js"></script>
</body>
</html> In the browser arbitrary: The provided type information works in conjunction with tools that work with JavaScript at the size of the application. Translation of React.mdtitle: React translatable: trueTypeScript isJSXsupports, React project setupNowadays, there are a lot of frameworks that support TypeScript as standard: All of this is a great framework as the first step in the project.This websiteTypeScript andGatsbyand this will also be a useful implementation reference. documentHere are some of the best sources to stay up to date on React and TypeScript: Translation of Migrating from JavaScript.mdtitle: Migrating from JavaScript oneline: How to migrate from JavaScript to TypeScriptTypeScript doesn't exist on its own. If you're thinking of migrating in a React project, start withReact Conversion GuideWe recommend that you read the . Directory settingsIf you are writing javascript, it is likely that you are running JavaScript directly. In these cases, the written file will be used as input to TypeScript and will execute the output generated by TypeScript. You might bundle it, use another transpilla like Babel, or perform intermediate steps on JavaScript. Now assume that the directory is configured as follows:
Describe the configuration fileTypeScript manages project settings such as which files you want to include and what types of checks you want to perform {
"compilerOptions": {
"outDir": "./built",
"allowJs": true,
"target": "es5"
},
"include": ["./src/**/*"]
} Above, we specify a few points for TypeScript:
At this point, at the root of the project Benefits of early adoptionEven at this stage, there are some benefits that TypeScript can get from understanding your project.
TypeScript also warns you of unreachable code and labels. Each of these warnings Integration with build toolsThere may be more build steps in the pipeline. GulpIf you're using Gulp in any way, typescript andUsing Gulpand tutorials on integration with common build tools like Browserify, Babelify, and Uglify. WebpackIntegration with Webpack is very simple. npm install ts-loader source-map-loader And the following options module.exports = {
entry: "./src/index.ts",
output: {
filename: "./dist/bundle.js",
},
// webpack の出力をデバッグするためのソースマップを有効にします。
devtool: "source-map",
resolve: {
// 解決可能な拡張子として'.ts'と'.tsx'を追加します。
extensions: ["", ".webpack.js", ".web.js", ".ts", ".tsx", ".js"],
},
module: {
rules: [
// '.ts'または'.tsx'拡張子を持つすべてのファイルは'ts-loader'によって処理されます。
{ test: /\.tsx?$/, loader: "ts-loader" },
// 出力されるすべての'.js'ファイルは'source-map-loader'によって再処理されたソースマップを持ちます。
{ test: /\.js$/, loader: "source-map-loader" },
],
},
// その他のオプション...
}; Importantly, Another TypeScript loader in Webpackawesome-typescript-loaderBut the same is true. An example of using Webpack is:React and Webpack tutorialsYou can check it in. Migrate to TypeScript filesBy now, you're ready to start using typescript files. Have you finished this step? Of course, you might think this step is incorrect. If you think this is daft, you can tighten this behavior. If you intend to use the more restrictive settings available, it is best to enable them now (see below.Stricter checkscheck the ).) Removing errorsAs mentioned earlier, it is not unpredictable that you will get an error message after conversion. Import from ModulesAt first // Node/CommonJS用
declare function require(path: string): any; Or code like this: // RequireJS/AMD用
declare function define(...args: any[]): any; You could write , but it's better to remove these calls and use typescript syntax for import. First, typescript Node/CommonJS code such as: var foo = require("foo");
foo.doStuff(); Or suppose you have a RequireJS/AMD code like this: define(["foo"], function (foo) {
foo.doStuff();
}); In that case, you would write TypeScript code like this: import foo = require("foo");
foo.doStuff(); Retrieving declaration filesIf you start converting to TypeScript import, you probably npm install -S @types/lodash If Now you can import the lodash without any problems to get the exact completion. Export from moduleTypically, exporting from a module is module.exports.feedPets = function (pets) {
// ...
}; This can also be written as follows: export function feedPets(pets) {
// ...
} Sometimes I completely overwrite the exports object. var express = require("express");
var app = express(); Previously, you might have written: function foo() {
// ...
}
module.exports = foo; In TypeScript, function foo() {
// ...
}
export = foo; Arguments that are too/too littleSometimes you notice that you are calling a function with too many/too few arguments. function myCoolFunction() {
if (arguments.length == 2 && !Array.isArray(arguments[1])) {
var f = arguments[0];
var arr = arguments[1];
// ...
}
// ...
}
myCoolFunction(
function (x) {
console.log(x);
},
[1, 2, 3, 4]
);
myCoolFunction(
function (x) {
console.log(x);
},
1,
2,
3,
4
); In this case, you can use the overload of the function function myCoolFunction(f: (x: number) => void, nums: number[]): void;
function myCoolFunction(f: (x: number) => void, ...nums: number[]): void;
function myCoolFunction() {
if (arguments.length == 2 && !Array.isArray(arguments[1])) {
var f = arguments[0];
var arr = arguments[1];
// ...
}
// ...
} Two overload signatures Properties added sequentiallySome people find it more aesthetic to create an object and add properties immediately after: var options = {};
options.color = "red";
options.volume = 11; TypeScript is let options = {
color: "red",
volume: 11,
}; Also interface Options {
color: string;
volume: number;
}
let options = {} as Options;
options.color = "red";
options.volume = 11; or
|
Co-authored-by: Daiki Ihara <sasurau4@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
お待たせしました 🙏
Co-authored-by: Daiki Ihara <sasurau4@gmail.com>
ありがとうございます! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
LGTM
|
There was an issue merging, maybe try again uraway. Details |
ref: #3
files:
diff:
22bc95d