简体   繁体   English

将代码从 JavaScript 转换为 TypeScript

[英]Converting the code from JavaScript to TypeScript

I'm learning TypeScript and for the purposes of learning I try to rewrite the code written in JavaScript What will be the best way to use functions within several classes.我正在学习 TypeScript,为了学习目的,我尝试重写用 JavaScript 编写的代码在多个类中使用函数的最佳方法是什么。 I would like every class to have access to the function sayHello()我希望每个班级都可以访问函数 sayHello()

I'm trying to rewrite my JavaScript code into TypeScript我正在尝试将我的 JavaScript 代码重写为 TypeScript

JavaScript: JavaScript:

class Guest {
    firstName;
    lastName;
        
    constructor() { 
    } 
    
    static sayHello() {
        console.log(`Hi, my name is ${this.firstName}${this.lastName}`); 
    } 
} 
    
class Employee {
    firstName;
    lastName;
    permissionLevel;
    static sayHello() {
        console.log(`Hi, my name is ${ this.firstName }${ this.lastName }`);
    }
} 

class Director {

    firstName;
    lastName;

    constructor() {
        
    }
    static sayHello() {
        console.log(`Hi, my name is ${this.firstName}${this.lastName}`);
    }
}
    
let clerk = new Employee();
var director = new Director("John", "Smith"); 
Director.sayHello();
   

TypeScript打字稿

function sayHello(firstName: string, lastName: string): void {
    console.log(`Hi, my name is ${firstName}${lastName}`)
}

class Guest {
    firstName: string;
    lastName: string;
        
    constructor() { 
    } 

} 
    
class Employee {
    firstName: string;
    lastName: string;
    permissionLevel: string;
} 

class Director {
    firstName;
    lastName;

    constructor() {
        
    }
}
    
const clerk = new Employee();
const director = new Director("John", "Smith"); 
Director.sayHello();
    

I would like the sayHello () function to be available in every class.我希望每个班级都可以使用 sayHello() 函数。

Additionally, I am wondering whether to create an interface for firstName and lastName to make the code more readable另外,我想知道是否为 firstName 和 lastName 创建一个接口以使代码更具可读性

interface Person {
    firstName: string, 
    lastName: string
}

Could someone recommend me how to best translate this code to TypeScript.有人可以推荐我如何最好地将此代码转换为 TypeScript。

Your JavaScript code is not good either, static functions can't reach instance values.你的 JavaScript 代码也不好,静态函数无法到达实例值。

You could write something like this in JavaScript你可以用 JavaScript 写这样的东西

class Person {
  constructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  sayHello() {
    console.log(`Hi, my name is ${this.firstName ?? '-'} ${this.lastName ?? '-'} `);
  }
}

class Guest extends Person {}

class Employee extends Person {
  permissionLevel;
}

class Director extends Person {}

const clerk = new Employee();
clerk.sayHello();
const director = new Director('John', 'Smith');
director.sayHello();

Stackblitz JavaScript example Stackblitz JavaScript 示例

This code could be written is TypeScript like this:这段代码可以写成这样的 TypeScript:

class Person {
  // Add types here
  constructor(public firstName?: string, public lastName?: string) { }

  sayHello() {
    console.log(`Hi, my name is ${this.firstName ?? '-'} ${this.lastName ?? '-'} `);
  }
}

class Guest extends Person {}

class Employee extends Person {
  // Add types here
  permissionLevel?: number;
}

class Director extends Person {}

const clerk = new Employee();
clerk.sayHello();
const director = new Director('John', 'Smith');
director.sayHello();

Stackblitz TypeScript example Stackblitz TypeScript 示例

As you can see there's not much difference.如您所见,没有太大区别。

abstract class Person {
  
  firstName: string
  lastName: string
  
  constructor(firstName: string, lastName: string) {
    this.firstName = firstName
    this.lastName = lastName
  }
  
  sayHello(): void {
    console.log(`Hi, my name is ${ this.firstName } ${ this.lastName }`)
  }
  
}

class Employee extends Person {
  
  permissionLevel: string
  
  constructor(firstName: string, lastName: string, permissionLevel: string) {
    super(firstName, lastName)
    this.permissionLevel = permissionLevel
  }
  
}

class Director extends Person {}

var director = new Director("John", "Smith");
director.sayHello();
let clerk = new Employee("Al", "Clergy", "Clerk");
clerk.sayHello();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM