Skip to content

Latest commit

 

History

History
91 lines (53 loc) · 1.3 KB

介绍配置与语法基础.md

File metadata and controls

91 lines (53 loc) · 1.3 KB

返回目录

TypeScript 语法基础与工程实践

环境配置

$ Cannot find name 'require'. (2304)

$ npm install @types/node --save-dev

可以使用 ts-node 来直接运行 TypeScript 代码:

# Execute a script as you would normally with `node`.

ts-node script.ts



# Starts the TypeScript REPL.

ts-node



# Execute code with TypeScript.

ts-node -e 'console.log("Hello, world!")'



# Execute, and print, code with TypeScript.

ts-node -p '"Hello, world!"'



# Pipe scripts to execute with TypeScript.

echo "console.log('Hello, world!')" | ts-node

数据类型

interface Metadata {
  key: string;

  value: any;
}

interface MetadataObj {
  [key: string]: Metadata;
}

var metadata: MetadataObj = {
  // type annotation here is needed

  someReducer: { key: 'test', value: 1 },

  anotherReducer: { key: 'test', value: 2 } // ...
};

function doSomething(metadata: MetadataObj) {
  // ...
}

doSomething(metadata);
The property 'MyNamespace' does not exist on value of type 'window' any"
declare global {
  interface Window {
    MyNamespace: any;
  }
}

window.MyNamespace = window.MyNamespace || {};

window['MyNamespace'];

window['get'] = get;

window['get']...