Q:

typescript interface

type ValueOf<T> = T[keyof T];
2
interface IEmployee {
    empCode: number;
    empName: string;
    getSalary: (number) => number; // arrow function
    getManagerName(number): string; 
}
5
interface Task{
    name: String; //property
    run(arg: any):void; //method
}

class MyTask implements Task{
    name: String;
    constructor(name: String) {
        this.name = name;
    }
	run(arg: any): void {
        console.log(`running: ${this.name}, arg: ${arg}`);
    }
}

let myTask: Task = new MyTask('someTask');
myTask.run("test");
10
interface LabeledValue {
  label: string;
}

function printLabel(labeledObj: LabeledValue) {
  console.log(labeledObj.label);
}

let myObj = { size: 10, label: "Size 10 Object" };
printLabel(myObj);Try
0
class Animal {
    name: string;
    constructor(theName: string) { this.name = theName; }
    move(distanceInMeters: number = 0) {
        console.log(`${this.name} moved ${distanceInMeters}m.`);
    }
}

class Snake extends Animal {
    constructor(name: string) { super(name); }
    move(distanceInMeters = 5) {
        console.log("Slithering...");
        super.move(distanceInMeters);
    }
}

class Horse extends Animal {
    constructor(name: string) { super(name); }
    move(distanceInMeters = 45) {
        console.log("Galloping...");
        super.move(distanceInMeters);
    }
}

let sam = new Snake("Sammy the Python");
let tom: Animal = new Horse("Tommy the Palomino");

sam.move();
tom.move(34);
1
running: someTask, arg: test
0
interface NumberOrStringDictionary {
  [index: string]: number | string;
  length: number; // ok, length is a number
  name: string; // ok, name is a string
}Try
0
// use type instead interface
type StringOrNull = string | null;
0
//difference b/w type and interface

/////////////////////// types reference another types
type FormElement = React.FormEvent<HTMLFormElement>; 

// How to use?
 const handleSubmit = (e: FormElement): void => { ... }

 
/////////////////////// interface create new types and
///////////////////////	also we can extend other interfaces

interface MyTodo {
  text: string;
  complete: boolean;
}
                                                 
interface MyTodo2 extends MyTodo {
  // also have all values from MyTodo
  tags: string[]
}
                                                 
// How to use? 
const [todos, setTodos] = useState<MyTodo[]>([]);
 

-1

New to Communities?

Join the community