diff --git a/package-lock.json b/package-lock.json index c6a18e7..09d470f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,11 @@ "version": "1.20150623.0", "resolved": "https://registry.npmjs.org/tsc/-/tsc-1.20150623.0.tgz", "integrity": "sha1-Trw8d04WkUjLx2inNCUz8ILHpuU=" + }, + "typescript": { + "version": "4.2.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.2.3.tgz", + "integrity": "sha512-qOcYwxaByStAWrBf4x0fibwZvMRG+r4cQoTjbPtUlrWjBHbmCAww1i448U0GJ+3cNNEtebDteo/cHOR3xJ4wEw==" } } } diff --git a/package.json b/package.json index 0d703f8..33a40c3 100644 --- a/package.json +++ b/package.json @@ -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": {} } diff --git a/src/task_1/index.ts b/src/task_1/index.ts index 3edb7b9..cf24a55 100644 --- a/src/task_1/index.ts +++ b/src/task_1/index.ts @@ -3,8 +3,8 @@ * который написан в функции logObj компилировался и исполнялся корректно */ -type FooType = unknown; -type BarType = unknown; +type FooType = {stringProp: String, numberProp: Number, barObject: BarType}; +type BarType = {stringsArrayProp: Array, numbersOrDatesArrayProp: Array, functionProp(flag: boolean): void}; export const fooObjects: FooType[] = [ { diff --git a/src/task_2/index.ts b/src/task_2/index.ts index 00b4939..e7bb5e1 100644 --- a/src/task_2/index.ts +++ b/src/task_2/index.ts @@ -71,6 +71,7 @@ const obj7: ThirdType = { const array = [obj1, obj2, obj3, obj4, obj5, obj6, obj7]; function filter(array: Array, type: string) { + return array.filter((x): x is FirstType | SecondType | ThirdType => typeof x === type); } filter(array, 'FirstType'); diff --git a/src/task_3/index.ts b/src/task_3/index.ts index b7c9cdc..8a1e906 100644 --- a/src/task_3/index.ts +++ b/src/task_3/index.ts @@ -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 \ No newline at end of file diff --git a/src/task_4/index.ts b/src/task_4/index.ts index e884df8..b9e86a2 100644 --- a/src/task_4/index.ts +++ b/src/task_4/index.ts @@ -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 \ No newline at end of file diff --git a/src/task_5/index.ts b/src/task_5/index.ts index ef3e0c0..988e02e 100644 --- a/src/task_5/index.ts +++ b/src/task_5/index.ts @@ -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) => {