Skip to content

Commit

Permalink
Merge pull request #34 from typescript-tutorial-exercises/dev
Browse files Browse the repository at this point in the history
release-20240220
  • Loading branch information
ZLY201 committed Feb 20, 2024
2 parents d498430 + a4d8439 commit 05959fb
Show file tree
Hide file tree
Showing 63 changed files with 1,687 additions and 1,001 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"node": true,
"browser": true
},
"globals": {
"WEBPACK_IS_SSR": true
},
"plugins": [
"react",
"import",
Expand Down
2 changes: 2 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ Please follow the commit specification. See [`.commitlintrc.json`](https://githu
### Commonly used NPM scripts

```bash
# add new problem
$ yarn new
# run project in development mode
$ yarn dev
# build all dist files
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ node_modules
.npmignore
.DS_Store
yarn-error.log
output
3 changes: 1 addition & 2 deletions config/links.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"github-repo": "https://github.com/typescript-tutorial-exercises/core",
"github-zly201": "https://github.com/zly201"
"github-repo": "https://github.com/typescript-tutorial-exercises"
}
7 changes: 5 additions & 2 deletions config/problems.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
"key": "HelloWorld",
"subject": "Basic Tutorial",
"subjectKey": "BasicTutorial",
"title": "Hello, world!"
"title": "Hello, world!",
"author": "ZLY201"
},
{
"key": "EverydayTypes",
"subject": "Basic Tutorial",
"subjectKey": "BasicTutorial",
"title": "EveryDay Types"
"title": "EveryDay Types",
"author": "ZLY201"
},
{
"key": "TransposeMatrix",
"subject": "Difficulties",
"subjectKey": "Difficulties",
"title": "Transpose Matrix",
"author": "ZLY201",
"cases": [
{
"source": "[[1, 2]]",
Expand Down
5 changes: 3 additions & 2 deletions html/index.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<!DOCTYPE html>
<html lang="zh-CN">
<html lang='en'>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>TypeScript Tutorial Exercises</title>
</head>
<body>
<div id="root"></div>
<script>try{if(JSON.parse(localStorage.getItem('__setting_cache__')).theme==='dark'){document.body.setAttribute('arco-theme','dark');}}catch{}</script>
<div id="root">{% ROOT_CONTENT %}</div>
</body>
</html>
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"reset": "rm -rf node_modules",
"setup": "yarn reset && yarn",
"clean": "rm -rf dist",
"dev": "rspack serve --watch --mode=development",
"build": "yarn clean && rspack build --mode=production",
"dev": "rspack serve --config=tools/rspack.config.ts --watch",
"build:ssr": "rspack build --config=tools/rspack.ssr.config.ts",
"build": "yarn clean && yarn build:ssr && rspack build --config=tools/rspack.config.ts && rm -rf dist/ssr",
"new": "ts-node tools/addNewProblem.ts",
"lint": "eslint --fix --color --cache --quiet .",
"prepare": "husky install"
},
Expand Down Expand Up @@ -52,10 +54,12 @@
"devDependencies": {
"@arco-plugins/unplugin-react": "^1.0.0-alpha.1",
"@rspack/cli": "^0.3.5",
"@rspack/plugin-html": "^0.3.11",
"@svgr/webpack": "^8.1.0",
"@types/event-emitter": "^0.3.4",
"@types/lodash.debounce": "^4.0.7",
"@types/node": "^20.8.9",
"@types/prompts": "^2.4.9",
"@types/react": "^17.0.67",
"@types/react-copy-to-clipboard": "^5.0.6",
"@types/react-dom": "^17.0.21",
Expand All @@ -80,6 +84,7 @@
"lint-staged": "^14.0.1",
"monaco-editor": "^0.44.0",
"prettier": "^3.0.3",
"prompts": "^2.4.2",
"style-loader": "^3.3.3",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
Expand Down
6 changes: 6 additions & 0 deletions problems/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": ["../.eslintrc.json"],
"rules": {
"compat/compat": "off"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ Now, you can go to the editor at right of the page to start your TypeScript trav

**Reference**

> 1.https://www.typescriptlang.org/
>
> 2.https://www.w3schools.com/typescript/typescript_intro.php
>
> 3.https://github.com/typescript-exercises/typescript-exercises
> 1.[https://www.typescriptlang.org/](https://www.typescriptlang.org/)
>
> 2.[https://www.w3schools.com/typescript/typescript_intro.php](https://www.w3schools.com/typescript/typescript_intro.php)
>
> 3.[https://github.com/typescript-exercises/typescript-exercises](https://github.com/typescript-exercises/typescript-exercises)
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ TypeScript 允许指定代码中传递的数据类型,并且能够在类型不

**参考**

> 1.https://www.typescriptlang.org/
> 1.[https://www.typescriptlang.org/](https://www.typescriptlang.org/)
>
> 2.https://www.w3schools.com/typescript/typescript_intro.php
> 2.[https://www.w3schools.com/typescript/typescript_intro.php](https://www.w3schools.com/typescript/typescript_intro.php)
>
> 3.https://github.com/typescript-exercises/typescript-exercises
> 3.[https://github.com/typescript-exercises/typescript-exercises](https://github.com/typescript-exercises/typescript-exercises)
6 changes: 6 additions & 0 deletions problems/basic-tutorial/2-everyday-types/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ assert<Equal<typeof constant.str, string>>();
// @ts-ignore
assert<Equal<typeof constant.num, number>>();
// @ts-ignore
assert<Equal<typeof constant.big_int, bigint>>();
// @ts-ignore
assert<Equal<typeof constant.bool, boolean>>();
// @ts-ignore
assert<Equal<typeof constant.mySymbol, symbol>>();
// @ts-ignore
assert<Equal<typeof constant.undef, undefined>>();
// @ts-ignore
assert<Equal<typeof constant.arr, number[]>>();
// @ts-ignore
assert<Equal<Parameters<typeof constant.greet>[0], string>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ Please replace all unknown and any in the code on the right with the correct typ

**Reference**

> https://www.typescriptlang.org/docs/handbook/2/everyday-types.html
> [https://www.typescriptlang.org/docs/handbook/2/everyday-types.html](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html)
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

**参考**

> https://www.typescriptlang.org/docs/handbook/2/everyday-types.html
> [https://www.typescriptlang.org/docs/handbook/2/everyday-types.html](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html)
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
```typescript
export const str: string = 'Hello, world';
export const num: number = 42;
export const big_int: bigint = 1n;
export const bool: boolean = true || false;
export const arr: number[] = [1];
export const mySymbol: symbol = Symbol('mySymbol');
export const undef: undefined = undefined;
export const arr: number[] = [1, 2, 3];

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

// should be `string` or `number`
export function printId(id: string | number) {
if (typeof id === 'string') {
// In this branch, id is of type 'string'
Expand Down
6 changes: 5 additions & 1 deletion problems/basic-tutorial/2-everyday-types/template.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
export const str: unknown = 'Hello, world';
export const num: unknown = 42;
export const big_int: unknown = 1n;
export const bool: unknown = true || false;
export const arr: unknown = [];
export const mySymbol: unknown = Symbol('mySymbol');
export const undef: unknown = undefined;
export const arr: unknown = [1, 2, 3];

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

// should be `string` or `number`
export function printId(id: unknown) {
if (typeof id === 'string') {
// In this branch, id is of type 'string'
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
### What is TypeScript

TypeScript is a syntactic superset of JavaScript which adds static typing.

This basically means that TypeScript adds syntax on top of JavaScript, allowing developers to add types.

### Why TypeScript

JavaScript is a loosely typed language. It can be difficult to understand what types of data are being passed around in JavaScript.

In JavaScript, function parameters and variables don't have any information! So developers need to look at documentation, or guess based on the implementation.

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.

For example, TypeScript will report an error when passing a string into a function that expects a number. JavaScript will not.

### About this site

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/).

This problem require you to export a type which equals to string `Hello, world`.

Now, you can go to the editor at right of the page to start your TypeScript travel.

**Reference**

> 1.https://www.typescriptlang.org/
>
> 2.https://www.w3schools.com/typescript/typescript_intro.php
>
> 3.https://github.com/typescript-exercises/typescript-exercises
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
### 关于 TypeScript

TypeScript 是 JavaScript 的语法超集,它添加了静态类型

这意味着 TypeScript 在 JavaScript 之上添加了语法,允许开发人员添加类型

### 为什么选择 TypeScript

JavaScript 是一种松散类型语言。 理解 JavaScript 中传递的数据类型可能很困难

在 JavaScript 中,函数参数和变量没有任何信息!因此开发人员需要查看文档,或者根据实现进行猜测

TypeScript 允许指定代码中传递的数据类型,并且能够在类型不匹配时报告错误

例如,当将字符串传递到需要数字的函数时,TypeScript 将报告错误而 JavaScript 不会

### 关于本站

为了教会更多的人使用 TypeScript(包括基本和高级用法),我们创建了这个网站,根据 [TypeScript 官方文档](https://www.typescriptlang.org/)提供了从入门到精通 TypeScript 的练习题

这个问题要求你导出一个等于字符串`Hello, world`的类型

现在,您可以前往页面右侧的编辑器开始学习 TypeScript

**参考**

> 1.https://www.typescriptlang.org/
>
> 2.https://www.w3schools.com/typescript/typescript_intro.php
>
> 3.https://github.com/typescript-exercises/typescript-exercises
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```typescript
export type HelloWorld = 'Hello, world';
```
6 changes: 6 additions & 0 deletions problems/basic-tutorial/3-narrowing/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import check from './check?url';
import template from './template?url';
import * as description from './docs/description';
import * as tutorial from './docs/tutorial';

export { check, template, tutorial, description };
8 changes: 8 additions & 0 deletions problems/basic-tutorial/3-narrowing/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function padLeft(padding: unknown, input: string) {
if (typeof padding === 'number') {
return ' '.repeat(padding) + input;
} else if (typeof padding === 'string') {
return padding + input;
}
return input;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import en from './description.en.md?url';
import zhCN from './description.zh-cn.md?url';

export { en, zhCN };
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import en from './tutorial.en.md?url';

export { en };
File renamed without changes.
2 changes: 1 addition & 1 deletion problems/difficulties/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * as TransposeMatrix from './transpose-matrix';
export * as TransposeMatrix from './1-transpose-matrix';
9 changes: 9 additions & 0 deletions problems/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare module '*?raw' {
const raw: string;
export default raw;
}

declare module '*?url' {
const url: string;
export default url;
}
10 changes: 10 additions & 0 deletions problems/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"baseUrl": "..",
"target": "ESNext",
"noImplicitAny": false,
},
"include": ["."],
"exclude": []
}
3 changes: 3 additions & 0 deletions problems/type-assertions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import typeAssertions from '../node_modules/type-assertions/lib/index.d.ts?raw';

export default typeAssertions;
Loading

0 comments on commit 05959fb

Please sign in to comment.