Giter VIP home page Giter VIP logo

typescript-section2-6-9's Introduction

typescript-section2-"Arrays, OOP, Interfaces"

Arrays

To indicate that a variable is an array type in typescript you indicate the type and then add "[]" after to indicate that is an array of whatever type you told it.

In this lesson, persons is an array of objects:

const persons:{}[] = [{...}]

But anything can be added to show that it is an "something" array:

const traxigon: number[] const traxigon: string[] const traxigon: booleen[]

Back to Persons Array, we indicate that the objects that make up the array should be constructed like so:

const persons:{name: string job: string age: number}[]

Line 19

In the line:

document.getElementById("app")!.innerHTML=<ul>${liListString}</ul>

"!" is used to indicate that the document.getElementById('app') should not return as Null or undefined. For reasons, typescript shows that when retrieving that element, it can either return as HTML or as Null.

! is used to add your opinion to the code, and indicate that this thing should be null.

Interfaces

Interfaces in Typescript are used to create custom Types. In our persons example, a person in the persons object array is:

{ name: string job: string age: number }

We use an interface to create a type so that we don't need to continually copy and paste that a person has a name, job, and age. Like so:

interface Person {
  name: string
  job: string
  age: number
  isHappy?: boolean
  // We can even add an array inside the interface - the one below expects our Person typed array
  friendList?: Person[]
  // Functions can be added as well.
  sayHi?: () => void
}

const persons: Person[] = [
  { name: 'Alice', job: 'Front-end Dev', age: 28 },
  { name: 'Bob', job: 'Back-end Dev', age: 28 },
  { name: 'Codiku', job: 'Full Stack Dev', age: 28 }
]

typescript-section2-6-9's People

Contributors

ajm24027 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.