Untitled

 avatar
unknown
plain_text
10 months ago
1.7 kB
2
Indexable
// public, private, protected


interface PersonInterface{
    id: number
    name: string

    register(): string
}

// implements to use personinterface in the class

class Person implements PersonInterface{
    id: number
    name: string

    constructor(id: number, name: string) {
        this.id = id
        this.name = name
    }
    
    register() {
        return `${this.name} is now registered`
    }
}

const brad = new Person(1, "Brad")
console.log(brad)

// sub class siden den extender person. Person er superclass
class Employee extends Person {
    position: string

    constructor(id: number, name: string, position: string) {
        // instead of typing this.id = id etc, use "super" to get them from the superclass. as this extends it. 
        super(id, name)
        this.position = position

    }
}

const emp = new Employee(3, 'Shawn', "Developer")

// siden employee extender person, får den metoden også.
console.log(emp.register())

// Generics. Se nedover så skjønner du. istedenfor Any så putter du generic. Så når du "initializer" 
// arrayet sier du hvilken type som går inn og ut. 
function getArray1(items: any[]): any[]{
    return new Array().concat(items)
}

let numArray = getArray1([1,2,3,4])
let strArray = getArray1(["brad", "john", "jill"])

numArray.push('hello')


function getArray2<T>(items: T[]): T[]{
    return new Array().concat(items)
}

let numArray2 = getArray2<number>([1,2,3,4])
let strArray2 = getArray2<string>(["brad", "john", "jill"])

// numArray2.push('hello')

// typescript with react SE ETTER DU HAR LÆRT REACT. 
Editor is loading...
Leave a Comment