node默认找.js,解析ts须要安装css
npm i ts-node -ghtml
target: es5
用tsc默认选择es3node
若是在命令行中指定了配置文件,tsconfig会被忽略jquery
module: commonjs tsc ./src/es6/a.ts -t es3 -t target 简写webpack
target为es3 es5,module默认指定为es6git
tsc ./src/es6/a.ts -m amdes6
-m module 简写github
tsc util.ts -d 可生成对应的声明文件 exports.c = 3 次级导出 module.exports = {} 顶级导出web
esModuleInterop: true import c4 = require("./es6/d") 既能够用import = 方式导入 又能够用import from 方式导入npm
namespace 用export 导出到全局
命名空间和模块不要混用,不要在一个模块中使用命名空间, 命名空间最好在一个全局的环境中使用
命名空间成员的别名问题: import circle = Shape.circle console.log(circal(2))
interface A { x: number; foo (bar: number): number; // 3 _5 foo (bar: 'a'): number // _2 } interface A { y: number; foo (bar: string): string // 1 _3 foo (bar: number[]): number[] // 2 _4 foo (bar: 'b'): number // _1 } let a: A = { x: 1, y: 2, foo(bar: any){ return bar } }
function Lib() {} namespace Lib { export let version = "1.0" } console.log(Lib.version) // 1.0 - 至关于声明了一个Lib函数,在函数里面导出了一个属性
class C {} namespace C { export let state = 1 } console.log(C.state) 至关于给类添加了一个静态属性state
enum Color { Red, Yellow, Blue } namespace Color { export function min(){ } // 就至关于给这个枚举类型增长了一个方法 //
npm i @types/jquery -D
能够在这里搜索: https://microsoft.github.io/TypeSearch/
// index.ts import $ from 'jquery' $('.app').css('color','red') globaleLib({x: 1}) globaleLib.dosomething() import moduleLib from './module-lib' moduleLib.dosomething() import umdLib from './umd-lib'
declare 关键字
// 声明: declare function globalLib(options: globalLib.options): void interface Options { [key: string]: key } declare namespace globleLib { const version: string; function doSomething(): void; interface Options { [key: string]: key } } export = globleLib
// 源码: function globalLib(options) { console.log(options) } globalLib.version = '1.0.0' globalLib.doSomething = function(){ console.log('globalLib do something') }
declare function moduleLib(options: moduleLib.options): void // 由于声明文件自己是一个模块,因此放在这里不会暴露在全局 interface Options { [key: string]: key } declare namespace moduleLib { // ts官网中,多了个export export const version: string; function doSomething(): void; } export = moduleLib
// 声明文件 declare nmdLib { const version: string function dosomething(): void } // 若是要编写一个umd库 这个语句是不可缺乏的 export as namespace umdLib export = umdLib
// 源码: (function (root, factory) { if (typeof define === "function" && define.amd) { define(factory); } else if (typeof module === "object" && module.exports) { module.exports = factory(); } else { root.umdLib = factory(); } }(this, function() { return { version: '1.0.0', doSomething() { console.log('umdLib do something'); } } }));
import m from 'moment' declare module 'moment' { export function myFunction(): void } m.myFunction = () => {}
// 会对全局变量形成必定污染 declare global { namespace globalLib { function doAnything(): void } } globalLib.doAnything = () => {}
- jquery的模块依赖 mode_modules/@types/jquery/package.json types: 'index' ---> index.d.ts // 模块依赖,使用types属性,ts会在@types目录下寻找此sizzle, // ts会把sizzle的声明文件引入进来 /// <reference types="sizzle" /> // 路径依赖,使用path属性,是个相对的路径 /// <reference path="JQueryStatic.d.ts" /> /// <reference path="JQuery.d.ts" /> /// <reference path="misc.d.ts" /> /// <reference path="legacy.d.ts" />