@@ -190,17 +190,29 @@
Types
type StrinOrNumber = string | number
+
+
type StringOrNumberOrBoolean = StringOrNumber | boolean
Modifiers
- const a: string
- const b?: string
- const c!: string
+ const a?: string // string | undefined
- constructor(field?: text)
+ const obj = {
+ a: string,
+ b?: number,
+ }
+
+ funntion log(msg?: string) {}
+
+
+ class {
+ b!: string
+ }
+
+ obj!.one!.two!.three
@@ -229,7 +241,7 @@
}
const pizza: Pizza = {
- slices: -1, https:// ❌
+ slices: -1, // ❌
round: true,
name: 'Margherita'
}
@@ -302,6 +314,23 @@
}
+
+ Classes
+
+ class User {
+ private id: string
+ public readonly username: string
+ friends: User[] = []
+
+ constructor() {
+ this.id = 'abc'
+ this.username = 'John Doe'
+ }
+
+ private doSomeStuff() {}
+ }
+
+
Generics
@@ -338,9 +367,9 @@
b: string;
};
- const x: Props = { a: 5 }; https:// Error: property 'b' missing
+ const x: Props = { a: 5 }; // Error: property 'b' missing
- const y: Partial<Props> = { a: 5 }; https:// OK
+ const y: Partial<Props> = { a: 5 }; // OK
interface Props {
@@ -348,9 +377,9 @@
b?: string;
};
- const x: Props = { a: 5 }; https:// OK
+ const x: Props = { a: 5 }; // OK
- const y: Required<Props> = { a: 5 }; https:// Error: property 'b' missing
+ const y: Required<Props> = { a: 5 }; // Error: property 'b' missing
@@ -364,7 +393,7 @@
title: 'Delete inactive users',
};
- todo.title = 'Hello'; https:// Error: cannot reassign a readonly property
+ todo.title = 'Hello'; // Error: cannot reassign a readonly property
@@ -429,7 +458,7 @@
class Auto {
- wheels: number
+ wheels!: number
constructor(init: typeof Auto) {
Object.assign(this, init)
@@ -443,7 +472,7 @@
class Auto {
wheels?: number
doors?: number
- https:// ...
+ // ...
constructor(init: Partial<Auto>) {
Object.assign(this, init)
@@ -467,7 +496,7 @@
class Auto {
wheels!: number
doors?: number
- https:// ...
+ // ...
constructor(init: RequireSome<Auto, 'wheels'>) {
Object.assign(this, init)