JS + Static Types
const div = (a, b) => a / b;
div(4, 2) === 2
div(4, "abc") === NaN
const div = (a: number, b: number): number => a / b;
div(4, 2) === 2
div(4, "abc") // ⚡️ abc is not of type `number`
const div = (a: number, b: number) => a / b;
div(4, 2) === 2
div(4, "abc") // ⚡️ abc is not of type `number`
➡️ gradual adoption
number
string
boolean
true
& false
"bla"
)1
, 2
)
interface SomeType {
member1: boolean;
member2: HTMLElement;
member3: string;
member4: "bla";
}
interface SomeType {
member1: boolean; // can be true / false
member2: HTMLElement; // can be any HTML element
member3: string; // can be any string
member4: "bla"; // can be *just* the string "bla"
}
"This variable has all members of both types"
TypeA & TypeB
, "TypeA
and TypeB
"
const a = { x: 123 }; // type { x: number }
const b = { y: 456 }; // type { y: number }
const a = { x: 123 }; // type { x: number }
const b = { y: 456 }; // type { y: number }
const aAndB = { ...a, ...b };
const a = { x: 123 }; // type { x: number }
const b = { y: 456 }; // type { y: number }
// type { x: number, y: number } = typeof a & typeof b
const aAndB = { ...a, ...b };
"This variable is either of type A or of type B"
TypeA | TypeB
, "TypeA
or TypeB
"
const select = () => {
const a = { variant: 'A', x: 123 };
const b = { variant: 'B', y: 456 };
};
const select = () => {
// type { variant: 'A', x: number }
const a = { variant: 'A', x: 123 };
// type { variant: 'B', y: number }
const b = { variant: 'B', y: 456 };
};
const select = () => {
// type { variant: 'A', x: number }
const a = { variant: 'A', x: 123 };
// type { variant: 'B', y: number }
const b = { variant: 'B', y: 456 };
return Math.random() > 0.5 ? a : b;
};
// returns either a or b, so:
// typeof a | typeof b =
// | { variant: 'A', x: number }
// | { variant: 'B', y: number }
const select = () => {
// type { variant: 'A', x: number }
const a = { variant: 'A', x: 123 };
// type { variant: 'B', y: number }
const b = { variant: 'B', y: 456 };
return Math.random() > 0.5 ? a : b;
};
type A = typeof a = { variant: 'A', x: number };
type B = typeof b = { variant: 'B', y: number };
const getStr = (item: A | B): string => {
};
const getStr = (item: A | B): string => {
switch (item.variant) {
case "A":
case "B":
}
};
const getStr = (item: A | B): string => {
switch (item.variant) {
case "A":
console.log(item.x); // ✅
case "B":
}
};
const getStr = (item: A | B): string => {
switch (item.variant) {
case "A":
console.log(item.x); // ✅
case "B":
console.log(item.y); // ✅
}
};
const getStr = (item: A | B): string => {
switch (item.variant) {
case "A":
console.log(item.x); // ✅
console.log(item.y); // ⚡️
case "B":
console.log(item.y); // ✅
console.log(item.x); // ⚡️
}
};
const getStr = (item: A | B): string => {
switch (item.variant) {
case "A":
console.log(item.x); // ✅
console.log(item.y); // ⚡️
return String(item.x);
case "B":
console.log(item.y); // ✅
console.log(item.x); // ⚡️
return String(item.y);
}
};
const getStr = (item: A | B): string => {
switch (item.variant) {
case "A":
console.log(item.x); // ✅
console.log(item.y); // ⚡️
return String(item.x);
case "B":
console.log(item.y); // ✅
console.log(item.x); // ⚡️
return String(item.y);
// no `default:`-case, but that's okay because
// the switch is exhaustive!
}
};
tsc
can treat null
and undefined
as concrete typesstring !== string | null
null
-casenull
-errors 🎉
const trim = (param: string | null) => {
return param.trim(); // ⚡️ param can be `null`
if (!param) {
// this branch must diverge
return '';
}
return param.trim(); // ✅ no complaints from tsc
};
$ npx create-react-app my-app --typescript
$ yarn create react-app my-app --typescript
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing