学习-五分钟上手ts

网址:https://www.tslang.cn/docs/handbook/typescript-in-5-minutes.html

// 函数重新实现 解决方法 在文件夹中创建tsconfig.json即可
function hello(name:string) {
    return 'hello' + name
}
// let user = [0,1,2]
let user = 'yibo'
document.body.innerHTML = hello(user);
/**
 * 类型注解:
 * TypeScript里的类型注解是一种轻量级的为函数或变量添加约束的方式。在这个例子
 * 中,我们希望hello函数接收一个字符串参数。然后尝试把hello的调用改成传入一个
 * 数组:
 * 红色波浪线提示:
 * 类型“number[]”的参数不能赋给类型“string”的参数。
 * 要注意的是尽管有错误,index.js文件还是被创建了。就算代码里有错误,仍然可以
 * 使用TypeScript。但TypeScript会警告代码可能不会按预期执行。
 */

/**
 * tsc index.ts在命令行上,运行TypeScript编译器:
 */

/**
 * 接口
 * 
 */
//  interface Person {
//     firstName: String;
//     lastName:String;
// }
// function greeter(Person:Person) {
//     return 'hello' + Person.firstName + '' + Person.lastName
// }
// let fullName = {firstName: 'li', lastName: 'si'}
// document.body.innerHTML = greeter(fullName)
/**
 * 类
 */
class Student {
    fullName: string;
    constructor(public firstName, public middleInitial, public lastName) {
        this.fullName = firstName + '' + middleInitial + '' + lastName;
    }
}
interface Person {
    firstName: string;
    lastName: string;
}
function greeter(person:Person) {
    return 'hello' + person.firstName + '' + person.lastName
}

let User = new Student('Jane','M.','User')
document.body.innerHTML = greeter(User)

 

posted on 2023-04-04 17:39  法老的微笑  阅读(97)  评论(0)    收藏  举报