Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/task_1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* Требуется описать типы FooType и BarType так, чтобы код,
* который написан в функции logObj компилировался и исполнялся корректно
*/

type FooType = unknown;
type BarType = unknown;
type numOrDate = Date | number;
type BarType = {stringsArrayProp: Array<string>, numbersOrDatesArrayProp: Array<numOrDate>, functionProp: Function};
type FooType = {stringProp: string, numberProp: number, barObject: BarType};

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачтено на 3 балла

export const fooObjects: FooType[] = [
{
Expand Down
4 changes: 3 additions & 1 deletion src/task_2/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/** Задача 2
* Требуется реализовать функцию filter, которая будет принимать
* массив с объектами 3х типов
Expand Down Expand Up @@ -71,8 +72,9 @@ const obj7: ThirdType = {
const array = [obj1, obj2, obj3, obj4, obj5, obj6, obj7];

function filter(array: Array<FirstType | SecondType | ThirdType>, type: string) {

}

filter(array, 'FirstType');
filter(array, 'SecondType');
filter(array, 'ThirdType');
filter(array, 'ThirdType');
12 changes: 10 additions & 2 deletions src/task_3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачтено на 3 балла

14 changes: 14 additions & 0 deletions src/task_4/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачтено на 3 балла

2 changes: 1 addition & 1 deletion src/task_5/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачтено на 3 балла

Expand Down