typescript / beginner
Snippet
Type Assertions
Type assertions are a way to tell the compiler to treat a value as a specific type when you have more information about it than TypeScript does.
snippet.ts
1
2
const rawData: any = '123';const dataLength = (rawData as string).length;
Breakdown
1
(rawData as string)
Tells the compiler to treat the 'any' type variable as a string.
2
.length
Accesses the string property 'length' which is now known to be available.