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
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
},
"homepage": "https://github.com/RTF-Angular-2021/ts-task-1#readme",
"dependencies": {
"tsc": "^1.20150623.0"
"tsc": "^1.20150623.0",
"typescript": "^4.2.3"
},
"devDependencies": {}
}
4 changes: 2 additions & 2 deletions src/task_1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* который написан в функции logObj компилировался и исполнялся корректно
*/

type FooType = unknown;
type BarType = unknown;
type FooType = {stringProp: String, numberProp: Number, barObject: BarType};
type BarType = {stringsArrayProp: Array<string>, numbersOrDatesArrayProp: Array<Date | number>, functionProp(flag: boolean): void};

export const fooObjects: FooType[] = [
{
Expand Down
1 change: 1 addition & 0 deletions src/task_2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const obj7: ThirdType = {
const array = [obj1, obj2, obj3, obj4, obj5, obj6, obj7];

function filter(array: Array<FirstType | SecondType | ThirdType>, type: string) {
return array.filter((x): x is FirstType | SecondType | ThirdType => typeof x === type);
}

filter(array, 'FirstType');
Expand Down
4 changes: 4 additions & 0 deletions src/task_3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
*/
function add(x: string, y: string): string;
function add(x: number, y: number): number;
function add(x:any, y:any):any{
return x+y;
};


add('20', '21'); //2021
add(20, 21); //41
54 changes: 54 additions & 0 deletions src/task_4/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,57 @@ type FooType = {
a: number
b: string
};

/*
Приведу аналогию, пожалуй. Type это struct в C#, только не имеющий инициализатора/конструктора
Class can implement Interface, types
Class can be extended by Interface and classes
Variable can implement type
Variable cannot be extended by any of type or interface
*/

interface IAnimal{
DoSound(): void
name: string
sex: string //better enum
}

type DogType ={
breed: string;
name: string|Number;
doSound(): void;
}

class Dog implements IAnimal{
DoSound(): void {
console.log("Whoooff!");
}
name: "Axel";
sex: "Male";
breed: "HolyWater";
}

//But cannot compile class when extending type

class DogT extends DogType{ //Compile error
doSound(): void {
throw new Error("Method not implemented.");
}
breed: string;
name: 21312;

}

//At the same time, can compile class when implementing type

class DogTI implements DogType{
doSound(): void {
throw new Error("Method not implemented.");
}
breed: string;
name: 21312;

}

//type cannot be extended by another type or interface
//Interface can be extended by another interface
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) => {
Expand Down