Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript Exercise #140

Open
xgqfrms opened this issue Jan 15, 2023 · 1 comment
Open

TypeScript Exercise #140

xgqfrms opened this issue Jan 15, 2023 · 1 comment
Labels
TypeScript Exercise TypeScript Exercise TypeScript TypeScript

Comments

@xgqfrms
Copy link
Owner

xgqfrms commented Jan 15, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TypeScript Exercise TypeScript Exercise TypeScript TypeScript
Projects
None yet
Development

No branches or pull requests

1 participant