摘要:
多个命名空间的引用 components.ts namespace Components{ export interface User{ name: string } export class Header { constructor() { const elem = document.create 阅读全文
摘要:
demo.ts class Header { constructor() { const elem = document.createElement('div'); elem.innerText = 'This is Header'; document.body.appendChild(elem); 阅读全文
摘要:
class DataManager{ constructor(private data: string[]) {} getItem(index: number): string { return this.data[index] } } /** * 创建了这样一个类,传递进来一个 string类型的 阅读全文
摘要:
function join(first: string | number, second: string | number) { return `${first}${second}`; } join('1', 1); /** * 这么看 join 还挺好用的。 * 如果我想做到这两个数要么都传 st 阅读全文
摘要:
function getResult(status) { if (status 0) { return 'offline' } else if (status 1) { return 'online'; } else if (status 2) { return 'deleted' } return 阅读全文
摘要:
interface Bird{ fly: boolean, sing: ()=>{} } interface Dog{ fly: boolean, bark: ()=>{} } /** * animal: Bird | Dog这就是一个联合类型 * animal 既可以是 Bird 又可以是 Dog 阅读全文
摘要:
tsc --init 会生成一个 tsconfig.json 的配置文件,tsc 没法用需要全局安装 typescript。这个文件是对 ts 的编译配置文件。我们新建一个文件夹,生成 tsconfig.json,将 "removeComments": true, 这个配置放开,在编译的时候,把注释 阅读全文
摘要:
1、新建文件夹 demo 2、npm init -y 生成 package.json 文件 3、tsc --init 生成 tsconfig.json,可以对 ts 的默认配置做修改 4、npm install -D(--save-dev) ts-node npm install -D typesc 阅读全文
摘要:
类属性只能读不能写的两种方式 class Person { constructor(public name:string){} } const person = new Person('zina'); person.name = 'hello'; // 这里可以修改 name console.log 阅读全文
摘要:
class Person { constructor(private _name: string){} // 对于私有的属性进行处理后再暴露出去,比如加密,确保安全 get name(){ return this._name + ' hi'; } // 外层无法直接赋值,通过 set 赋值 set 阅读全文