Answers for "typescript class interface"

6

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)
Posted by: Guest on December-07-2020
1

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];
Posted by: Guest on September-29-2021
12

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");
Posted by: Guest on February-24-2020
1

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;
}
Posted by: Guest on July-19-2021
3

typescript type interface

//INTERFACE	                                TYPE
interface Animal {	                        type Animal = {
    name: string;	                            name: string;
}	                                        }
interface Bear extends Animal {	            type Bear = Animal & { 
    honey: boolean;	                            honey: Boolean;
}	                                        }

const bear = getBear();	                    const bear = getBear();
bear.name;	                                bear.name;
bear.honey;	                                bear.honey;
Posted by: Guest on April-24-2021
1

typescript interface

interface LabeledValue {
  label: string;
}

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

let myObj = { size: 10, label: "Size 10 Object" };
printLabel(myObj);Try
Posted by: Guest on December-07-2020

Code answers related to "typescript class interface"

Code answers related to "TypeScript"

Browse Popular Code Answers by Language