Skip to content

Commit 05959fb

Browse files
authored
Merge pull request #34 from typescript-tutorial-exercises/dev
release-20240220
2 parents d498430 + a4d8439 commit 05959fb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1687
-1001
lines changed

.eslintrc.json

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"node": true,
55
"browser": true
66
},
7+
"globals": {
8+
"WEBPACK_IS_SSR": true
9+
},
710
"plugins": [
811
"react",
912
"import",

.github/CONTRIBUTING.md

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ Please follow the commit specification. See [`.commitlintrc.json`](https://githu
4949
### Commonly used NPM scripts
5050

5151
```bash
52+
# add new problem
53+
$ yarn new
5254
# run project in development mode
5355
$ yarn dev
5456
# build all dist files

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ node_modules
77
.npmignore
88
.DS_Store
99
yarn-error.log
10-
output

config/links.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
{
2-
"github-repo": "https://github.com/typescript-tutorial-exercises/core",
3-
"github-zly201": "https://github.com/zly201"
2+
"github-repo": "https://github.com/typescript-tutorial-exercises"
43
}

config/problems.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@
33
"key": "HelloWorld",
44
"subject": "Basic Tutorial",
55
"subjectKey": "BasicTutorial",
6-
"title": "Hello, world!"
6+
"title": "Hello, world!",
7+
"author": "ZLY201"
78
},
89
{
910
"key": "EverydayTypes",
1011
"subject": "Basic Tutorial",
1112
"subjectKey": "BasicTutorial",
12-
"title": "EveryDay Types"
13+
"title": "EveryDay Types",
14+
"author": "ZLY201"
1315
},
1416
{
1517
"key": "TransposeMatrix",
1618
"subject": "Difficulties",
1719
"subjectKey": "Difficulties",
1820
"title": "Transpose Matrix",
21+
"author": "ZLY201",
1922
"cases": [
2023
{
2124
"source": "[[1, 2]]",

html/index.html

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<!DOCTYPE html>
2-
<html lang="zh-CN">
2+
<html lang='en'>
33
<head>
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>TypeScript Tutorial Exercises</title>
77
</head>
88
<body>
9-
<div id="root"></div>
9+
<script>try{if(JSON.parse(localStorage.getItem('__setting_cache__')).theme==='dark'){document.body.setAttribute('arco-theme','dark');}}catch{}</script>
10+
<div id="root">{% ROOT_CONTENT %}</div>
1011
</body>
1112
</html>

package.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
"reset": "rm -rf node_modules",
1111
"setup": "yarn reset && yarn",
1212
"clean": "rm -rf dist",
13-
"dev": "rspack serve --watch --mode=development",
14-
"build": "yarn clean && rspack build --mode=production",
13+
"dev": "rspack serve --config=tools/rspack.config.ts --watch",
14+
"build:ssr": "rspack build --config=tools/rspack.ssr.config.ts",
15+
"build": "yarn clean && yarn build:ssr && rspack build --config=tools/rspack.config.ts && rm -rf dist/ssr",
16+
"new": "ts-node tools/addNewProblem.ts",
1517
"lint": "eslint --fix --color --cache --quiet .",
1618
"prepare": "husky install"
1719
},
@@ -52,10 +54,12 @@
5254
"devDependencies": {
5355
"@arco-plugins/unplugin-react": "^1.0.0-alpha.1",
5456
"@rspack/cli": "^0.3.5",
57+
"@rspack/plugin-html": "^0.3.11",
5558
"@svgr/webpack": "^8.1.0",
5659
"@types/event-emitter": "^0.3.4",
5760
"@types/lodash.debounce": "^4.0.7",
5861
"@types/node": "^20.8.9",
62+
"@types/prompts": "^2.4.9",
5963
"@types/react": "^17.0.67",
6064
"@types/react-copy-to-clipboard": "^5.0.6",
6165
"@types/react-dom": "^17.0.21",
@@ -80,6 +84,7 @@
8084
"lint-staged": "^14.0.1",
8185
"monaco-editor": "^0.44.0",
8286
"prettier": "^3.0.3",
87+
"prompts": "^2.4.2",
8388
"style-loader": "^3.3.3",
8489
"ts-node": "^10.9.1",
8590
"typescript": "^5.2.2"

problems/.eslintrc.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": ["../.eslintrc.json"],
3+
"rules": {
4+
"compat/compat": "off"
5+
}
6+
}

problems/basic-tutorial/1-hello-world/docs/description/description.en.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ Now, you can go to the editor at right of the page to start your TypeScript trav
2424

2525
**Reference**
2626

27-
> 1.https://www.typescriptlang.org/
28-
>
29-
> 2.https://www.w3schools.com/typescript/typescript_intro.php
30-
>
31-
> 3.https://github.com/typescript-exercises/typescript-exercises
27+
> 1.[https://www.typescriptlang.org/](https://www.typescriptlang.org/)
28+
>
29+
> 2.[https://www.w3schools.com/typescript/typescript_intro.php](https://www.w3schools.com/typescript/typescript_intro.php)
30+
>
31+
> 3.[https://github.com/typescript-exercises/typescript-exercises](https://github.com/typescript-exercises/typescript-exercises)
3232

problems/basic-tutorial/1-hello-world/docs/description/description.zh-cn.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ TypeScript 允许指定代码中传递的数据类型,并且能够在类型不
2424

2525
**参考**
2626

27-
> 1.https://www.typescriptlang.org/
27+
> 1.[https://www.typescriptlang.org/](https://www.typescriptlang.org/)
2828
>
29-
> 2.https://www.w3schools.com/typescript/typescript_intro.php
29+
> 2.[https://www.w3schools.com/typescript/typescript_intro.php](https://www.w3schools.com/typescript/typescript_intro.php)
3030
>
31-
> 3.https://github.com/typescript-exercises/typescript-exercises
31+
> 3.[https://github.com/typescript-exercises/typescript-exercises](https://github.com/typescript-exercises/typescript-exercises)

problems/basic-tutorial/2-everyday-types/check.ts

+6
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ assert<Equal<typeof constant.str, string>>();
66
// @ts-ignore
77
assert<Equal<typeof constant.num, number>>();
88
// @ts-ignore
9+
assert<Equal<typeof constant.big_int, bigint>>();
10+
// @ts-ignore
911
assert<Equal<typeof constant.bool, boolean>>();
1012
// @ts-ignore
13+
assert<Equal<typeof constant.mySymbol, symbol>>();
14+
// @ts-ignore
15+
assert<Equal<typeof constant.undef, undefined>>();
16+
// @ts-ignore
1117
assert<Equal<typeof constant.arr, number[]>>();
1218
// @ts-ignore
1319
assert<Equal<Parameters<typeof constant.greet>[0], string>>();

problems/basic-tutorial/2-everyday-types/docs/description/description.en.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ Please replace all unknown and any in the code on the right with the correct typ
22

33
**Reference**
44

5-
> https://www.typescriptlang.org/docs/handbook/2/everyday-types.html
5+
> [https://www.typescriptlang.org/docs/handbook/2/everyday-types.html](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html)

problems/basic-tutorial/2-everyday-types/docs/description/description.zh-cn.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
**参考**
44

5-
> https://www.typescriptlang.org/docs/handbook/2/everyday-types.html
5+
> [https://www.typescriptlang.org/docs/handbook/2/everyday-types.html](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html)

problems/basic-tutorial/2-everyday-types/docs/tutorial/tutorial.en.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
```typescript
22
export const str: string = 'Hello, world';
33
export const num: number = 42;
4+
export const big_int: bigint = 1n;
45
export const bool: boolean = true || false;
5-
export const arr: number[] = [1];
6+
export const mySymbol: symbol = Symbol('mySymbol');
7+
export const undef: undefined = undefined;
8+
export const arr: number[] = [1, 2, 3];
69

710
export function greet(name: string): void {
811
return console.log('Hello, ' + name.toUpperCase() + '!!');
@@ -13,6 +16,7 @@ export function printName(obj: { first: string; last?: string }): string {
1316
return `${last}${first}`;
1417
}
1518

19+
// should be `string` or `number`
1620
export function printId(id: string | number) {
1721
if (typeof id === 'string') {
1822
// In this branch, id is of type 'string'

problems/basic-tutorial/2-everyday-types/template.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
export const str: unknown = 'Hello, world';
22
export const num: unknown = 42;
3+
export const big_int: unknown = 1n;
34
export const bool: unknown = true || false;
4-
export const arr: unknown = [];
5+
export const mySymbol: unknown = Symbol('mySymbol');
6+
export const undef: unknown = undefined;
7+
export const arr: unknown = [1, 2, 3];
58

69
export function greet(name: any): unknown {
710
return console.log('Hello, ' + name.toUpperCase() + '!!');
@@ -12,6 +15,7 @@ export function printName(obj: { first: unknown; last?: unknown }): unknown {
1215
return `${last}${first}`;
1316
}
1417

18+
// should be `string` or `number`
1519
export function printId(id: unknown) {
1620
if (typeof id === 'string') {
1721
// In this branch, id is of type 'string'

problems/basic-tutorial/3-narrowing/check.ts

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
### What is TypeScript
2+
3+
TypeScript is a syntactic superset of JavaScript which adds static typing.
4+
5+
This basically means that TypeScript adds syntax on top of JavaScript, allowing developers to add types.
6+
7+
### Why TypeScript
8+
9+
JavaScript is a loosely typed language. It can be difficult to understand what types of data are being passed around in JavaScript.
10+
11+
In JavaScript, function parameters and variables don't have any information! So developers need to look at documentation, or guess based on the implementation.
12+
13+
TypeScript allows specifying the types of data being passed around within the code, and has the ability to report errors when the types don't match.
14+
15+
For example, TypeScript will report an error when passing a string into a function that expects a number. JavaScript will not.
16+
17+
### About this site
18+
19+
To teach more people to use TypeScript including basic and advanced usage, we create this site which provides some problems should be solved by TypeScript. And part of the answers of the problems could be found at [official TypeScript document](https://www.typescriptlang.org/).
20+
21+
This problem require you to export a type which equals to string `Hello, world`.
22+
23+
Now, you can go to the editor at right of the page to start your TypeScript travel.
24+
25+
**Reference**
26+
27+
> 1.https://www.typescriptlang.org/
28+
>
29+
> 2.https://www.w3schools.com/typescript/typescript_intro.php
30+
>
31+
> 3.https://github.com/typescript-exercises/typescript-exercises
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
### 关于 TypeScript
2+
3+
TypeScript 是 JavaScript 的语法超集,它添加了静态类型
4+
5+
这意味着 TypeScript 在 JavaScript 之上添加了语法,允许开发人员添加类型
6+
7+
### 为什么选择 TypeScript
8+
9+
JavaScript 是一种松散类型语言。 理解 JavaScript 中传递的数据类型可能很困难
10+
11+
在 JavaScript 中,函数参数和变量没有任何信息!因此开发人员需要查看文档,或者根据实现进行猜测
12+
13+
TypeScript 允许指定代码中传递的数据类型,并且能够在类型不匹配时报告错误
14+
15+
例如,当将字符串传递到需要数字的函数时,TypeScript 将报告错误而 JavaScript 不会
16+
17+
### 关于本站
18+
19+
为了教会更多的人使用 TypeScript(包括基本和高级用法),我们创建了这个网站,根据 [TypeScript 官方文档](https://www.typescriptlang.org/)提供了从入门到精通 TypeScript 的练习题
20+
21+
这个问题要求你导出一个等于字符串`Hello, world`的类型
22+
23+
现在,您可以前往页面右侧的编辑器开始学习 TypeScript
24+
25+
**参考**
26+
27+
> 1.https://www.typescriptlang.org/
28+
>
29+
> 2.https://www.w3schools.com/typescript/typescript_intro.php
30+
>
31+
> 3.https://github.com/typescript-exercises/typescript-exercises
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```typescript
2+
export type HelloWorld = 'Hello, world';
3+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import check from './check?url';
2+
import template from './template?url';
3+
import * as description from './docs/description';
4+
import * as tutorial from './docs/tutorial';
5+
6+
export { check, template, tutorial, description };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function padLeft(padding: unknown, input: string) {
2+
if (typeof padding === 'number') {
3+
return ' '.repeat(padding) + input;
4+
} else if (typeof padding === 'string') {
5+
return padding + input;
6+
}
7+
return input;
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import en from './description.en.md?url';
2+
import zhCN from './description.zh-cn.md?url';
3+
4+
export { en, zhCN };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import en from './tutorial.en.md?url';
2+
3+
export { en };

problems/difficulties/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * as TransposeMatrix from './transpose-matrix';
1+
export * as TransposeMatrix from './1-transpose-matrix';

problems/global.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
declare module '*?raw' {
2+
const raw: string;
3+
export default raw;
4+
}
5+
6+
declare module '*?url' {
7+
const url: string;
8+
export default url;
9+
}

problems/tsconfig.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"baseUrl": "..",
5+
"target": "ESNext",
6+
"noImplicitAny": false,
7+
},
8+
"include": ["."],
9+
"exclude": []
10+
}

problems/type-assertions.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import typeAssertions from '../node_modules/type-assertions/lib/index.d.ts?raw';
2+
3+
export default typeAssertions;

0 commit comments

Comments
 (0)