typescript interface function
interface IEmployee {
empCode: number;
empName: string;
getSalary: (number) => number; // arrow function
getManagerName(number): string;
}
typescript interface function
interface IEmployee {
empCode: number;
empName: string;
getSalary: (number) => number; // arrow function
getManagerName(number): string;
}
typescript class interface
interface IPerson {
name: string
age: number
hobby?: string[]
}
class Person implements IPerson {
name: string
age: number
hobby?: string[]
constructor(name: string, age: number, hobby: string[]) {
this.name = name
this.age = age
this.hobby = hobby
}
}
const output = new Person('john doe', 23, ['swimming', 'traveling', 'badminton'])
console.log(output)
typescript interface
interface Foo {
bar: string;
qux: number;
}
// Creates object implementing interface:
const MyFoo = <Foo> {
bar: "Hello",
qux: 7
}
// Or:
const MyFoo: Foo = {
bar: "Hello",
qux: 7
}
typescript interface
// https://stackoverflow.com/a/41967120
// `I` Prefix not recommended :
// Ref : https://stackoverflow.com/a/41967120,
// https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines
interface SquareConfig {
color?: string;
width?: number;
[propName: string]: any;
}
interface StringArray {
[index: number]: string;
}
let myArray: StringArray;
myArray = ["Bob", "Fred"];
let myStr: string = myArray[0];
typescript class implements interface
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");
typescript interface function
type ErrorHandler = (error: IError) => void // type for only one function
// or
interface IErrorHandler {
ErrorHander: (error: IError) => void
}
// IError Interface if interest
interface IError {
error: string;
status: number;
message: string;
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us