0% found this document useful (0 votes)
4 views7 pages

X 2 3

Uploaded by

juhbaldissera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

X 2 3

Uploaded by

juhbaldissera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

function processData(data: any) {

data.toLowerCase();
if (typeof data === 'string') {
console.log(`It's string: ${data}`)
} else if (typeof data === 'number') {
console.log(`It's number: ${data}`)
}
}

function processData2(data: unknown) {


// data.toLowerCase();
if (typeof data === 'string') {
data.toLowerCase();
console.log(`It's string: ${data}`)
} else if (typeof data === 'number') {
console.log(`It's number: ${data}`)
}
}

function serializeAmount(amount: string | number): number {


if (typeof amount === 'string') {
return parseInt(amount);
} else {
return amount;
}
}

// type Circle = { radius: number };


// type Square = { x: number };
// type Triangle = { x: number; y: number };

// type Shape = Circle | Square | Triangle;

// const isCircle = (value: Shape): value is Circle =>


// value && typeof value === "object" && value['radius'];

// const isTriangle = (value: Shape): value is Triangle =>


// value && typeof value === "object" && value['x'] && value['y'];

// function area(s: Shape) {


// if (isCircle(s)) {
// return Math.PI * s.radius * s.radius;
// }
// else if (isTriangle(s)) {
// return (s.x * s.y) / 2;
// } else {
// return s.x * s.x;
// }
// }

// type Circle = { kind: "circle"; radius: number };


// type Square = { kind: "square"; x: number };
// type Triangle = { kind: "triangle"; x: number; y: number };

// type Shape = Circle | Square | Triangle;

// function area(s: Shape) {


// if (s.kind === "circle") {
// return Math.PI * s.radius * s.radius;
// }
// else if (s.kind === "square") {
// return s.x * s.x;
// } else {
// return (s.x * s.y) / 2;
// }
// }

// let value: string | number;


// value = 'hello';
// if (typeof value === 'string') {
// console.log(value.toUpperCase());
// }
// value = 42;
// if (typeof value === 'number') {
// console.log(value.toFixed(2));
// }

class Square {
constructor(public width: number) {}
}
class Rectangle {
constructor(public width: number, public height: number ) {}
}

function area(shape: Square | Rectangle) {


if (shape instanceof Square) {

return shape.width * shape.width;


} else {

return shape.width * shape.height;


}
}
const square = new Square(5);
const rectangle = new Rectangle(5, 10);
console.log(area(square)); // 25
console.log(area(rectangle)); // 50

// type = {
// name: string;
// age: number;
// };
// type UpdatePersonType = Partial<PersonType>;

// type PersonResponseType = Promise<{ name: string; age: number; }>;


// type AwaitedPersonResponseType = Awaited<PersonResponseType>;
// type UpdatePersonType = {
// name?: string;
// age?: number;
// };

// type PersonType = Required<UpdatePersonType>;

// type PersonType = {
// name: string;
// age: number;
// };
// type ReadonlyPersonType = Readonly<PersonType>;
// const person: ReadonlyPersonType = { name: 'Simon', age: 17 };
// person.name = 'John';

// type getPerson = (id: number) => { name: string; age: number; };


// type PersonType = ReturnType<getPerson>;

class Person {
constructor(
public name: string,
public age: number
) {}
}
type PersonConstructorParams = ConstructorParameters<typeof Person>;

const params: PersonConstructorParams = ['John', 30];


const person = new Person(...params);

// type PersonType = {
// name: string;
// age: number;
// };

// type getPerson = (id: number) => PersonType;

// type getPersonParamsType = Parameters<getPerson>;

// type ProductType = {
// name: string;
// price: number;
// };

// const products: Record<string, ProductType> = {


// apple: { name: 'Apple', price: 0.5 },
// banana: { name: 'Banana', price: 0.25 },
// };
// type ProductType = {
// name: string;
// price: number;
// currency: string;
// };

// type PriceType = Omit<ProductType, 'name'>;

// type FruitsType = 'banana' | 'apple' | 'pineapple';

// type YellowFruitsType = Extract<FruitsType, 'banana' | 'pineapple'>;

// type FruitsType = 'banana' | 'apple' | 'pineapple';

// type YellowFruitsType = Exclude<FruitsType, 'apple'>;

type BirthdayType = Date | undefined | null;

type NonNullableBirthdayType = NonNullable<BirthdayType>;

type ProductType = {
name: string;
price: number;
}
type ReviewType = {
rating: 0 | 1 | 2 | 3 | 4 | 5;
comment: string;
}
type ProductReviewType = ProductType & ReviewType;

const productReview: ProductReviewType = {


name: 'shoes',
price: 50,
rating: 5,
comment: 'good product'
}
// function loggingArgs<T>(arg: T): T {
// console.log(arg);
// return arg;
// }

// let output = loggingArgs<string>("myString");

// function loggingArgs<T>(args: T[]): T[] {


// console.log(args.join());
// return args;
// }

// let output = loggingArgs<string>(["a", "b"]);

// function loggingArgs<T extends object >(arg: T): T {


// console.log(JSON.stringify(arg));
// return arg;
// }

// type BasicArgType = {
// date: Date;
// level: 'info' | 'error' | 'debug';
// obj: any;
// };

// let output = loggingArgs<BasicArgType>({ date: new Date(), level: 'info', obj:


'test'});

// type BasicArgType = {
// date: Date;
// level: 'info' | 'error' | 'debug';
// obj: any;
// };

// function loggingArgs<T extends BasicArgType = BasicArgType>(arg: T): T {


// console.log(` ${arg.date} - ${arg.level} = ${arg.obj}`);
// return arg;
// }

// let output = loggingArgs({ date: new Date(), level: 'info', obj: 'test'});

// type IsArray<T> = T extends any[] ? true : false;

// const myArray = [1, 2, 3];


// const myNumber = 42;

// type IsMyArrayAnArray = IsArray<typeof myArray>;


// type IsMyNumberAnArray = IsArray<typeof myNumber>;

// const point = { x: 10, y: 3 };


// type Point = typeof point;

type Department = 'it' | 'hr';


type Language = 'english' | 'french';

type DepartmentLanguageId = `${Department}-${Language}-id`;

// type OptionsFlags<Type> = {
// [Property in keyof Type]: boolean;
// };

// type Features = {
// darkMode: () => void;
// silenceMode: () => void;
// };

// type FeaturesConfigType = OptionsFlags<Features>;

// type FeaturesConfigType = {
// [key: string]: boolean;
// };

// const features: FeaturesConfigType = {


// darkMode: true,
// silenceMode: false,
// }

type FeatureFunction = (input: any) => any;

type Features = {
appearanceMode: (input: 'dark' | 'light' | 'auto') => void;
silenceMode: (input: boolean) => void;
};

type OptionsFlags<Type extends Record<string, FeatureFunction>> = {


[Property in keyof Type]: Type[Property] extends (...args: infer A) => void ?
A[0] : never;
};

type FeaturesConfigType = OptionsFlags<Features>;


const PeoplesList = [
{ name: "Alice", age: 15 },
{ name: "Bob", age: 23 },
{ name: "Eve", age: '38' },
];

type PersonType = typeof PeoplesList[number];


type AgeType = PersonType["age"];

type Point = { x: number; y: number };


type Coordinate = keyof Point;

const coordinate: Coordinate = 'x';

let value: unknown = false;


let strValue: string = <string>value;

console.log(strValue.toLowerCase());

You might also like