// Cast the "unknown" variable myVar as a string, using the `as` keyword:
let myVar: unknown = "Hello world!";
// var as type✅
console.log((myVar as string).length);
// Cast the "unknown" variable myVar as a string, using `< >`:
let myVar: unknown = "Hello world!";
// <type>var ✅
console.log((<string>myVar).length);
// Declare an object kindPerson from the Person interface, where all the properties are `optional`:
interface Person {
age: number;
firstName: string;
lastName: string;
}
// Partial ??? 属性可选
let kindPerson : Partial<Person> = {};
// Declare an object kindPerson from the Person interface, where all the properties are `required`.
interface Person {
age: number;
firstName: string;
lastName?: string;
}
// 必须滴 ✅
let kindPerson : Required<Person> = {
age: 1800,
firstName: "Santa",
lastName: "Claus"
};
TypeScript Exercise
https://www.w3schools.com/typescript/exercise.php?filename=exercise_casting1
https://www.w3schools.com/typescript/exercise.php?filename=exercise_casting2
https://www.w3schools.com/typescript/exercise.php?filename=exercise_utility_types1
https://www.w3schools.com/typescript/exercise.php?filename=exercise_utility_types2
https://www.w3schools.com/typescript/exercise.php?filename=exercise_utility_types3
refs
https://www.cnblogs.com/xgqfrms/p/17053678.html