xxxxxxxxxx
type Props = {
item: CartItemType;
addToCart: (clickedItem: CartItemType) => void;
removeFromCart: (id: number) => void;
};
xxxxxxxxxx
type Person = {
name: string;
age: number;
};
const person: Person {
named: 'Rex'
age: 23
}
function greet(person: Person) {
return "Hello " + person.name;
}
xxxxxxxxxx
// cannot use object for type defination because this is not recommended
// use Record<string, any> this same with object
const name: string = "john doe"
const age: number = 30
const days1: string[] = ["sunday","monday","thuesday","wenesday"]
const numb1: number[] = [1,2,3,4,5]
const days2: Array<string> = ["sunday","monday","thuesday","wenesday"]
const numb2: Array<number> = [1,2,3,4,5]
const person: Record<string, any> = {
name: "john doe",
age: 30
}
async function name(): Promise<string> {
return "john doe"
}
name().then(console.log)
async function str(): Promise<string[]> {
return ["sunday","monday","thuesday","wenesday"]
}
str().then(console.log)
async function int(): Promise<int[]> {
return [1,2,3,4,5]
}
int().then(console.log)
async function objectValue(): Promise<Record<string, any>> {
const person: Record<string, any> = {
name: "john doe",
age: 30
}
return person
}
objectValue().then(console.log)
async function objectValueMulti(): Promise<Record<string, any>[]> {
const person: Record<string, any>[] = [{
name: "john doe",
age: 30
},{
name: "jane doe",
age: 30
}]
return person
}
objectValueMulti().then(console.log)
xxxxxxxxxx
var name: string = "Anna";
let notes: (number | string)[] = ["Get Food", 23, "Call the previous number when betting"];
xxxxxxxxxx
interface Person {
name: string;
age: number;
}
const person:Person {
name: "Rex",
age: 20
}
function greet(person: Person) {
return "Hello " + person.name;
}
xxxxxxxxxx
function identity<Type>(arg: Type): Type {
return arg;
}
let fun = identity<string>("hello world");
console.log(fun);
/*think of this as you can specify the type later*/
/*Directly specifying version of the above would be something like
function identity(arg: string): string {
return arg;
}
let fun = identity("hello world");
console.log(fun);
*/
xxxxxxxxxx
grunt.registerMultiTask('clean', function(this: SomeType) {
//...
});
xxxxxxxxxx
type numberOrString = number | string
type numberType = 1|2|3|4|5|6|7|8
type letterType = 'a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'i'
// https://www.typescriptlang.org/docs/handbook/2/types-from-types.html