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 examples #3

Merged
merged 1 commit into from
Jun 21, 2021
Merged
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
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
# Minesweeper

## Lecture 1 Minesweeper presentation

## Lecture 2 Github repository overview
## Secture 1 Introduction

### Minesweeper presentation

### Github repository overview
[Creating a new repository](https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/creating-a-repository-on-github/creating-a-new-repository)

## Lecture 3 Project init
### Project init
[Pull request](https://github.com/nickovchinnikov/minesweeper/pull/1)

## Lecture 4 Code-style tools Eslint and Prettier
### Code-style tools Eslint and Prettier
[Pull request](https://github.com/nickovchinnikov/minesweeper/pull/2)

### Typescript basic
### Parametric typing with generics
### Interfaces, Types and Union
### Unknown, never and Tuple
### UtilityTypes
[Pull request](https://github.com/nickovchinnikov/minesweeper/pull/3)
76 changes: 76 additions & 0 deletions examples/Types/examples.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
type UserPermissions = 'admin' | 'user' | 'manager';

type PermissionsWithoutAdmin = Exclude<UserPermissions, 'admin'>;

interface DepartmentsForPermission {
depName: string;
lvl: number;
}

const DepsForPerms: Record<UserPermissions, DepartmentsForPermission> = {
admin: {
depName: 'security',
lvl: 10,
},
user: {
depName: 'sales',
lvl: 5,
},
manager: {
depName: 'sales',
lvl: 10,
},
};

type TuplePermissinons = [UserPermissions, UserPermissions];

type BasicUser<A = boolean, P = TuplePermissinons> = {
name: string;
surname: string;
age: number;
isAdmin: A;
permissions?: P;
};

type BasicUserWithoutPermissions = Omit<BasicUser, 'permissions'>;

type AdvancedUser = {
account: number;
};

type FullUser<A = boolean, P = string[]> = BasicUser<A, P> & AdvancedUser;

type BasicUserReadonly = Readonly<BasicUser>;
type BasicUserRequired = Required<BasicUser>;
type BasicUserPartial = Partial<BasicUser>;

type BasicUserReadonlyRequired = Readonly<Required<BasicUser>>;

const user: FullUser<boolean> = {
name: 'Nick',
surname: 'Ovchinnikov',
age: 30,
isAdmin: true,
account: 100,
permissions: ['admin', 'user'],
};

user.name = 'Test';

const usersArray: FullUser[] = [user, user];

function getFirst<T>(arr: T[]): T {
return arr[0];
}

type BasicFunction = () => FullUser[];

type getFirstReturnType = ReturnType<BasicFunction>;

getFirst<FullUser>(usersArray);

type MathFunc<T = number> = (a: T, b: T) => T;

const mul: MathFunc = (a, b) => a * b;

const add: MathFunc = (a, b) => a + b;