//Generics - обобщения
//const getter = (data: any):any => data;
function getter<T>(data: T): T {
return data;
}
console.log((getter<number>(10).toString().length))
type dd<T extends string | number | null> = T;
const azat: dd<string | number | null>[] = [];
azat.push(4, 's', null, 6, 6, 6, 6)
console.log(azat)
////////////////////////////////////////////////
function foo<T>(data: T): T {
return data
}
const out = foo<string>('Siiiiiiiiiii')
console.log(out)
//namer: T;
//sum1: (x: T, y: T) => T;
/////////////////////////////////////////////
class cl<T>{
constructor(public namer: T, public sum1: (x: T, y: T) => T) { }
}
let mine = new cl<number>(2, function (x, y) { return x + y });
mine.namer = 3111111111111111;
mine.sum1 = function (x, y) { return x + y }
const res = mine.sum1(10, 23)
console.log(mine.namer)
console.log(res)
Output
"use strict";
//Generics - обобщения
//const getter = (data: any):any => data;
function getter(data) {
return data;
}
console.log((getter(10).toString().length));
const azat = [];
azat.push(4, 's', null, 6, 6, 6, 6);
console.log(azat);
////////////////////////////////////////////////
function foo(data) {
return data;
}
const out = foo('Siiiiiiiiiii');
console.log(out);
//namer: T;
//sum1: (x: T, y: T) => T;
/////////////////////////////////////////////
class cl {
constructor(namer, sum1) {
this.namer = namer;
this.sum1 = sum1;
}
}
let mine = new cl(2, function (x, y) { return x + y; });
mine.namer = 3111111111111111;
mine.sum1 = function (x, y) { return x + y; };
const res = mine.sum1(10, 23);
console.log(mine.namer);
console.log(res);
Compiler Options
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"esModuleInterop": true,
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"target": "ES2017",
"jsx": "react",
"module": "ESNext",
"moduleResolution": "node"
}
}
Playground Link: Provided
class Me {
name:string;
age:number;
nick:string;
constructor(name:string,age:number,nick:string){
this.name=name;
this.age=age;
this.nick=nick
}
}
const Azat = new Me('Azat',22,'pezda');
console.log(Azat)
Output
"use strict";
class Me {
constructor(name, age, nick) {
this.name = name;
this.age = age;
this.nick = nick;
}
}
const Azat = new Me('Azat', 22, 'agent_47');
console.log(Azat);
Compiler Options
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"esModuleInterop": true,
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"target": "ES2017",
"jsx": "react",
"module": "ESNext",
"moduleResolution": "node"
}
}
Playground Link: Provided
class Me {
name:string;//free access
age:number=22;//access only in the class
nick:string='azat_hajy';//only daughters
pass:number=123;//for read only
constructor(name:string){
this.name=name;
}
getPass():string{
return `${this.name}' '${this.age}' '${this.nick}`
}
}
const azat = new Me('Azat');
console.log(azat.getPass())
Output
"use strict";
class Me {
constructor(name) {
this.age = 22; //access only in the class
this.nick = 'azat_hajy'; //only daughters
this.pass = 123; //for read only
this.name = name;
}
getPass() {
return `${this.name}' '${this.age}' '${this.nick}`;
}
}
const azat = new Me('Azat');
console.log(azat.getPass());
Compiler Options
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"esModuleInterop": true,
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"target": "ES2017",
"jsx": "react",
"module": "ESNext",
"moduleResolution": "node"
}
}
Playground Link: Provided
class Me {
constructor(
public name: string,//showing modificatiors is important
public age: number,
public nick: string,
public pass: number
) { }
}
Output
"use strict";
class Me {
constructor(name, //showing modificatiors is important
age, nick, pass) {
this.name = name;
this.age = age;
this.nick = nick;
this.pass = pass;
}
}
Compiler Options
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"esModuleInterop": true,
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"target": "ES2017",
"jsx": "react",
"module": "ESNext",
"moduleResolution": "node"
}
}
Playground Link: Provided
class Last {
constructor(
public name: string,
private age: number,
) {
this.name=name;
this.age=age;
}
change(l:number){
return this.age=l
}
set changeAge(k:number){
this.age=k;
}
}
const azat = new Last('Azat',22)
console.log(azat)
azat.change(12)
console.log(azat)
Output
"use strict";
class Last {
constructor(name, age) {
this.name = name;
this.age = age;
this.name = name;
this.age = age;
}
change(l) {
return this.age = l;
}
set changeAge(k) {
this.age = k;
}
}
const azat = new Last('Azat', 22);
console.log(azat);
azat.change(12);
console.log(azat);
Compiler Options
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"esModuleInterop": true,
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"target": "ES2017",
"jsx": "react",
"module": "ESNext",
"moduleResolution": "node"
}
}
Playground Link: Provided
class List {
static trunk = 1;
constructor(public name: string, private age: number) {
this.name=name;
this.age=age;
}
setAge(k:number){
this.age=k;
}
getPass() {
return `${List.name} ' ' ${List.trunk} ' ' ${this.age}`
}
}
const azat = new List('Azat', 21)
console.log(azat.getPass())
Output
"use strict";
class List {
constructor(name, age) {
this.name = name;
this.age = age;
this.name = name;
this.age = age;
}
setAge(k) {
this.age = k;
}
getPass() {
return `${List.name} ' ' ${List.trunk} ' ' ${this.age}`;
}
}
List.trunk = 1;
const azat = new List('Azat', 21);
console.log(azat.getPass());
Compiler Options
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"esModuleInterop": true,
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"target": "ES2017",
"jsx": "react",
"module": "ESNext",
"moduleResolution": "node"
}
}
Playground Link: Provided
//структурное прототипирование
//утиная типизация
//над объектная сущность, помогает описать форму объекта
interface User {
readonly name: string,//can't be changed
age?: number
}
const azat: User = {
name: 'azat',
}
console.log(azat.name,azat.age)
azat.age=22;
console.log(azat.name,' ',azat.age)
azat.name='a' //can't be changed
Output
"use strict";
//структурное прототипирование
//утиная типизация
const azat = {
name: 'azat',
};
console.log(azat.name, azat.age);
azat.age = 22;
console.log(azat.name, ' ', azat.age);
azat.name = 'a'; //can't be changed
Compiler Options
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"esModuleInterop": true,
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"target": "ES2017",
"jsx": "react",
"module": "ESNext",
"moduleResolution": "node"
}
}
Playground Link: Provided