-
Notifications
You must be signed in to change notification settings - Fork 0
/
16.TypeAssertions.ts
24 lines (18 loc) · 1.06 KB
/
16.TypeAssertions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// TypeScript Type Assertions:
// Sometimes, you might know the type of a value better than TypeScript does.
// In such cases, you can use type assertions to tell TypeScript about the exact type of a value.
// This doesn't change the runtime behavior of the value; it's simply a way to let TypeScript know your intent.
// For this example, let's assume we have a function `load()` that fetches data.
// We're not sure about the exact type it returns, but we expect it to be a string.
let data = load();
// Now, we want to use the `trim()` method which is available on strings.
// But TypeScript might complain if it's not sure that `data` is a string.
// This is where type assertions come in handy.
// Using the angle-bracket syntax for type assertion:
const trimmedData = (<string>data).trim();
// Note: There's another syntax for type assertions using the "as" keyword.
// const trimmedData = (data as string).trim();
// Now, `trimmedData` contains the trimmed version of the string fetched by `load()`.
function load(): any {
return " Sample loaded data with spaces ";
}