Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Typescript: é tipo javascript?

Typescript: é tipo javascript?

Gilson Doi Junior

February 27, 2020
Tweet

More Decks by Gilson Doi Junior

Other Decks in Programming

Transcript

  1. let result = isAnyTest( args) if (result === true) {

    whatToDoWhenTrue() } else { whatToDoWhenFalse() } O que faz isAnyTest? Será que ele garante retorno? Será que não é bom testar undefined e null
  2. app.use(async (ctx, next) => { User.save(ctx.body.username, ctx.body.password) }) Será que

    o todos as informações vieram corretas no body, params e query do request?
  3. function equation( a, b, c ) { a = Number(a)

    b = Number(b) c = Number(c) ... } Será que o todos as informações vieram corretas no body, params e query do request? let concatChars = 'b'+'a'+ +'a'+ 'a'
  4. Basic Types 1. Boolean 2. Number 3. String 4. Array

    5. Tuple 6. Enum 7. Any 8. Void 9. Null and Undefined 10. Never 11. Object
  5. Enum, Tuples e Types enum Roles {Admin, User, Client} type

    UserProfile = { type: Roles.Admin, department: string, project:string } | { type: Roles.User, project:string } | { type: Roles.Client, product:string }; function printProfile( user : UserProfile) { switch (user.type) { case Roles.Admin: return `department ${user.department} - project ${user.project}` case Roles.User: return `project ${user.project}` case Roles.Client: return `product ${user.product}` } } let x: [string, number] = ["hello", 10]
  6. Interface vs. Class Interface: • abstrato • não instanciável •

    mutável/implementável Classe: • concreto • construído • vem “pronto”
  7. Interface vs. Class enum Roles {Admin, User, Client} export interface

    UserInterface { login: string; password: string; profile?: Object; role: Roles; authenticate() : boolean } let interfacedUser : UserInterface; interfacedUser.login = "teste"; //erro interfacedUser.password = "senha"; //erro export class BaseUser { login: string; password: string; profile?: Object; role: Roles; constructor( login : string, password : string) : void { ... } authenticate() : boolean { if(UserDB.authenticate(this.login, this.password)) { return true; } else return false; } }
  8. Interface vs. Class class AdminUser implements UserInterface{ constructor( login :

    string, password : string ) { this.login = login; this.password = password; this.role = Roles.Admin; } authenticate() : boolean { if(this.login === "teste" && this.password === "senha") { return true; } else { return false; } } } class ClientUser extends BaseUser{ profile?: Object; role: Roles; constructor( login : string, password : string ) { super(login, password) this.role = Roles.Admin; } //não existe obrigação para sobrescrever o método }