xgqfrms / learning

learning : A collection of all kinds of resources, videos, pdf, blogs, codes... 📚 + 💻 + ❤
https://learning.xgqfrms.xyz
MIT License
16 stars 12 forks source link

TypeScript Exercise #140

Open xgqfrms opened 1 year ago

xgqfrms commented 1 year ago

TypeScript Exercise

// 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);

https://www.w3schools.com/typescript/exercise.php?filename=exercise_casting1

// Cast the "unknown" variable myVar as a string, using `< >`:

let myVar: unknown = "Hello world!";
// <type>var ✅
console.log((<string>myVar).length);

https://www.w3schools.com/typescript/exercise.php?filename=exercise_casting2

// 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> = {};

https://www.w3schools.com/typescript/exercise.php?filename=exercise_utility_types1

// 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"
};

https://www.w3schools.com/typescript/exercise.php?filename=exercise_utility_types2

// Complete the sentence:

// ✅
Record< string, number> is equivalent to { [key: string]: number }

https://www.w3schools.com/typescript/exercise.php?filename=exercise_utility_types3

refs

https://www.cnblogs.com/xgqfrms/p/17053678.html

xgqfrms commented 1 year ago

TypeScript Partial

https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype

https://dev.to/smpnjn/how-the-typescript-partial-type-works-2klj

refs

https://www.cnblogs.com/xgqfrms/p/16793600.html

https://www.cnblogs.com/xgqfrms/p/15877490.html