-
Notifications
You must be signed in to change notification settings - Fork 15
Сулейманов Эмиль #9
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,5 +8,13 @@ | |
| function add(x: string, y: string): string; | ||
| function add(x: number, y: number): number; | ||
|
|
||
| add('20', '21'); //2021 | ||
| add(20, 21); //41 | ||
| function add<T extends number | string>(x: T, y: T): any { | ||
| let type: T; | ||
| if (typeof type === "number") | ||
| return (x as number) + (y as number); | ||
| else | ||
| return (x as string) + (y as string); | ||
| } | ||
|
|
||
| console.log(add('20', '21')); //2021 | ||
| console.log(add(20, 21)); //41 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Зачтено на 3 балла |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,3 +12,17 @@ type FooType = { | |
| a: number | ||
| b: string | ||
| }; | ||
|
|
||
|
|
||
| // 1) Классы не могут наследоваться от type | ||
|
|
||
| // 2) С помощью type можно создавать объединение типов | ||
| type example = number; | ||
|
|
||
| type UnionWithType = FooType | example; | ||
| type UnionWithInterface = FooType | IFoo; | ||
|
|
||
| // 3) В интерфейсы можно добавить поля после создания, а в type нет | ||
| // https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#differences-between-type-aliases-and-interfaces | ||
|
|
||
| // 4) Интерфейсы не могут расширять объединение type | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Зачтено на 3 балла |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,7 +73,7 @@ export function logPerson(person: Person) { | |
| console.log(` - ${person.name}, ${person.age}, ${additionalInformation}`); | ||
| } | ||
|
|
||
| export function filterUsers(persons: Person[], criteria: User): User[] { | ||
| export function filterUsers(persons: Person[], criteria: {type?: string, name?:string, age?:number, occupation?:string}): User[] { | ||
| return persons.filter(isUser).filter((user) => { | ||
| const criteriaKeys = Object.keys(criteria); | ||
| return criteriaKeys.every((fieldName) => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Зачтено на 3 балла |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Зачтено на 3 балла