You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -451,6 +469,311 @@ let person: ReadonlyPerson = {
451
469
person.name='Jane Doe'; // Error: Cannot assign to 'name' because it is a read-only property
452
470
```
453
471
472
+
## Type Assertions
473
+
474
+
Type assertions allow you to tell TypeScript that you know more about the type of a value than it does. It's like a type cast but has no runtime impact.
475
+
476
+
```typescript
477
+
let someValue:unknown="Hello World";
478
+
let strLength:number= (someValueasstring).length;
479
+
480
+
// Alternatively using angle bracket syntax
481
+
let strLength2:number= (<string>someValue).length;
482
+
```
483
+
484
+
## Type Guards
485
+
486
+
Type guards are expressions that perform runtime checks to guarantee the type in a certain scope.
487
+
488
+
```typescript
489
+
function isString(value:unknown):valueisstring {
490
+
returntypeofvalue==="string";
491
+
}
492
+
493
+
function processValue(value:number|string) {
494
+
if (isString(value)) {
495
+
console.log(value.toUpperCase()); // TypeScript knows value is a string
496
+
} else {
497
+
console.log(value.toFixed(2)); // TypeScript knows value is a number
498
+
}
499
+
}
500
+
```
501
+
502
+
## Tuple Types
503
+
504
+
Tuples are arrays with a fixed number of elements whose types are known.
505
+
506
+
```typescript
507
+
let tuple: [string, number] = ["hello", 10];
508
+
console.log(tuple[0].substring(1)); // "ello"
509
+
console.log(tuple[1].toFixed(1)); // "10.0"
510
+
```
511
+
512
+
## Index Signatures
513
+
514
+
Index signatures allow you to create objects with flexible property names.
515
+
516
+
```typescript
517
+
interfaceStringMap {
518
+
[key:string]:string;
519
+
}
520
+
521
+
let map:StringMap= {
522
+
"foo": "bar",
523
+
"baz": "qux"
524
+
};
525
+
```
526
+
527
+
## Mapped Types
528
+
529
+
Mapped types allow you to create new types based on old ones by transforming properties.
530
+
531
+
```typescript
532
+
typeReadonly<T> = {
533
+
readonly [PinkeyofT]:T[P];
534
+
};
535
+
536
+
interfacePerson {
537
+
name:string;
538
+
age:number;
539
+
}
540
+
541
+
typeReadonlyPerson=Readonly<Person>;
542
+
```
543
+
544
+
## Conditional Types
545
+
546
+
Conditional types select one of two possible types based on a condition.
547
+
548
+
```typescript
549
+
typeTypeName<T> =
550
+
Textendsstring?"string":
551
+
Textendsnumber?"number":
552
+
Textendsboolean?"boolean":
553
+
"object";
554
+
555
+
typeT0=TypeName<string>; // "string"
556
+
typeT1=TypeName<number>; // "number"
557
+
```
558
+
559
+
## Union and Intersection Types
560
+
561
+
Union types allow a value to be one of several types, while intersection types combine multiple types into one.
562
+
563
+
```typescript
564
+
// Union type
565
+
typeStringOrNumber=string|number;
566
+
let value:StringOrNumber="hello";
567
+
value=42; // also valid
568
+
569
+
// Intersection type
570
+
interfaceHasName { name:string; }
571
+
interfaceHasAge { age:number; }
572
+
typePerson=HasName&HasAge;
573
+
574
+
let person:Person= {
575
+
name: "John",
576
+
age: 25
577
+
};
578
+
```
579
+
580
+
## Type Predicates
581
+
582
+
Type predicates are special return type annotations that declare a type guard.
583
+
584
+
```typescript
585
+
function isFish(pet:Fish|Bird):petisFish {
586
+
return (petasFish).swim!==undefined;
587
+
}
588
+
589
+
let pet:Fish|Bird=getRandomPet();
590
+
if (isFish(pet)) {
591
+
pet.swim(); // TypeScript knows pet is Fish
592
+
} else {
593
+
pet.fly(); // TypeScript knows pet is Bird
594
+
}
595
+
```
596
+
597
+
## Template Literal Types
598
+
599
+
Template literal types build on string literal types and allow creating complex string patterns.
600
+
601
+
```typescript
602
+
typeWorld="world";
603
+
typeGreeting=`hello ${World}`; // type Greeting = "hello world"
0 commit comments