typescript 点滴
<p>1 extend的用法</p><div class="cnblogs_code">
<pre>const x = extend({ a: 'hello' }, { b: 42 });</pre>
</div>
<p> 2只有在d.ts,你才可以使用 export as 这样子的语法。而且必须有namespace.</p>
<div class="cnblogs_Highlighter">
<pre class="brush:javascript;gutter:true;">export const aaa = 132;
export const bbb = 132;
export as namespace ccc;
</pre>
</div>
<p>3 至今也不知道 安装了@types/sizzle后。</p>
<div class="cnblogs_code">
<pre>export = Sizzle;</pre>
</div>
<p>4 </p>
<div class="cnblogs_Highlighter">
<pre class="brush:javascript;gutter:true;">class _BB {}
// 命名空间声明,用于代码提示
declare global {
declare namespace nn {
let BB: typeof _BB;
}
}
// 命名空间实际定义
window['nn'] = window.nn || {};
nn.BB = _BB
</pre>
</div>
<p>5</p>
<div class="cnblogs_code">
<pre>typeScript 有两种模块化方式,一种是使用 ES6 的 import/export 及其 TS 对这种语法的微小扩展;另一种方式是使用 TS 特有的 namespace (命名空间)。在分析这两种模块化方式之前,我先推荐使用第一种方式,因为第二种方式涉及到模块引用顺序的问题(可以通过 /<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> <reference path="..." /> 来解决,但感觉不如 import 爽。</span>
<span style="color: rgba(0, 0, 0, 1)">
如果使用 namespace 方式的模块化,那么所有东西都是全局的,内部引用直接使用即可,TS 能识别出同一命名空间下 export 的内容;外部引用导入即可;全局使用(比如在页面上的 </span><script><span style="color: rgba(0, 0, 0, 1)"> 内,把命名空间写完整就好(仅仍然只能使用 export 的内容。
如果使用 ES6 模块方式的模块化,目前最好的方式可能就是挂到 window 上了,如果是在 Node 下,就需要挂到 global 上。如果要兼容,就得写点代码来判断全局对象。一般来说,用 TypeScript 写代码,就已经决定了要模块化,除非很少的时候需要在页面的 </span><script> 中调用脚本中的某些对象,这种情况往 window 上挂就行。如果是要做为库来发布,tsc 是可以编译生成 .d.ts 文件的,如果是引用 js,那就不存在静态类型检查的问题;如果是引用 ts,那就以模块化的方式引用;如果想以全局的方式引用 ts,那就在在全局对象上挂一个入口对象,然后在文档里说明,使用前自己申明这个对象(不需要提供 .d.ts),也就几行代码的事情,也不算麻烦。比如</pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">declare global {
interface Window {
myEntry: EntryClass;
}
}</span> </pre>
</div>
<div class="cnblogs_Highlighter">
<pre class="brush:javascript;gutter:true;">declare namespace Aaa{
interface bbb {
}
}
export {Aaa }</pre>
</div>
<p>第一种方式引入。</p>
<div class="cnblogs_code">
<pre>import { Aaa } from './m1.d'<span style="color: rgba(0, 0, 0, 1)">;
const aaa: Aaa.bbb;</span></pre>
</div>
<p>第二种方式引入。</p>
<p>例子</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">declare namespace Aaa{
interface bbb {
}
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> export {Aaa } 第二种方式引入这里如果不注释为什么会报错。</span></pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">/ <reference path="./m1.d.ts" /><br>这个/// 不写好像也能检测到,只是为了表明先后顺序。<br></span>
const aaa: Aaa.bbb;</pre>
</div>
<p> </p>
<p> </p>
<p>6 declare 和declare global</p>
<p>两个的作用是一样的,但是在vue-cli的中用的是declare global而且不能去掉globa.</p>
<p>7 不使用declare也是可以的呀。也可以在其他模块中使用呀。在简单的typescript环境中。</p>
<p>可以全局使用,但是在vue-cli中则不可以全局使用。</p>
<p>8 namespace默认是全局的。</p>
<p>9 declare</p>
<div class="cnblogs_code">
<pre>你可以通过 declare 关键字,来告诉 TypeScript,你正在试图表述一个其他地方已经存在的代码(如:写在 JavaScript、CoffeeScript 或者是像浏览器和 Node.js 运行环境里的代码):<br>declare的后边不能使用=后,只能生命其他地方已经有的代码,写=号会报错。</pre>
</div>
<div class="cnblogs_Highlighter">
<pre class="brush:javascript;gutter:true;">环境声明就好像你与编译器之间的一个约定,如果这些没有在编译时存在,但是你却使用了它们,则事情将会在没有警告的情况下中断。
环境声明就好像是一个文档。如果源文件更新了,你应该同步更进。所以,当你使用源文件在运行时的新行为时,如果没有人更新环境声明,编译器将会报错。
</pre>
</div>
<p> </p>
<p>10 以下两个是等效声明, 第一个使用内联注解,第二个使用接口:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">以下两个是等效声明, 第一个使用内联注解,第二个使用接口:
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Sample A</span>
<span style="color: rgba(0, 0, 0, 1)">declare const myPoint: { x: number; y: number };
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Sample B</span>
<span style="color: rgba(0, 0, 0, 1)">interface Point {
x: number;
y: number;
}
declare const myPoint: Point;</span></pre>
</div>
<p> 11 生命同样的接口</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Lib a.d.ts</span>
<span style="color: rgba(0, 0, 0, 1)">interface Point {
x: number,
y: number
}
declare const myPoint: Point
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Lib b.d.ts</span>
<span style="color: rgba(0, 0, 0, 1)">interface Point {
z: number
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Your code</span>
let myPoint.z <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Allowed!</span></pre>
</div>
<p> </p>
<p> 12 enum的实现源码</p>
<p><img src="https://img2018.cnblogs.com/blog/947364/201907/947364-20190719185018331-1592348208.png"></p>
<p> </p>
<p> 13 type 的enum</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">enum Color {
Red,
Green,
Blue
}
enum Color {
DarkRed </span>= 3<span style="color: rgba(0, 0, 0, 1)">,
DarkGreen,
DarkBlue
}</span></pre>
</div>
<p> </p>
<p>14 生命函数的两种方式,一种是,注解方式,一种接口方式。</p>
<p>内联类型注解</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> variable annotation 变量注解</span>
<span style="color: rgba(0, 0, 0, 1)">let sampleVariable: { bar: number };
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> function parameter annotation 函数注解</span>
<span style="color: rgba(0, 0, 255, 1)">function</span> foo(sampleParameter: { bar: number }) {}</pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">interface Foo {
foo: string;
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Return type annotated as `: Foo`</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> foo(sample: Foo): Foo {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> sample;
}</span></pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">interface ReturnString {
(): string;
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 它可以表示一个返回值为 string 的函数:</span>
<span style="color: rgba(0, 0, 0, 1)">
declare const foo: ReturnString;
const bar </span>= foo(); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> bar 被推断为一个字符串。</span></pre>
</div>
<div class="cnblogs_code">
<pre>为了使指定可调用的类型签名更容易,TypeScript 也允许你使用简单的箭头函数类型注解。例如,在一个以 number 类型为参数,以 string 类型为返回值的函数中,你可以这么写:<br>const simple: (foo: number) => string = foo => foo.toString();</pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">可实例化仅仅是可调用的一种特殊情况,它使用 <code>new</code> 做为前缀。它意味着你需用使用 <code>new</code> 关键字去调用它:<br>interface CallMeWithNewToGetString {
</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> (): string;
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 使用</span>
<span style="color: rgba(0, 0, 0, 1)">declare const Foo: CallMeWithNewToGetString;
const bar </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Foo(); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> bar 被推断为 string 类型</span></pre>
</div>
<p> 15. keyof</p>
<p>http://www.softwhy.com/article-9151-1.html</p>
<p>16 字面量类型 使用实例</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 用于创建字符串列表映射至 `K: V` 的函数</span>
<span style="color: rgba(0, 0, 255, 1)">function</span> strEnum<T extends string>(o: Array<T>): { : K } {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> o.reduce((res, key) =><span style="color: rgba(0, 0, 0, 1)"> {
res </span>=<span style="color: rgba(0, 0, 0, 1)"> key;
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> res;
}, Object.create(</span><span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">));
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建 K: V</span>
const Direction = strEnum(['North', 'South', 'East', 'West'<span style="color: rgba(0, 0, 0, 1)">]);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建一个类型</span>
type Direction = keyof <span style="color: rgba(0, 0, 255, 1)">typeof</span><span style="color: rgba(0, 0, 0, 1)"> Direction;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 简单的使用</span>
<span style="color: rgba(0, 0, 0, 1)">let sample: Direction;
sample </span>= Direction.North; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Okay</span>
sample = 'North'; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Okay</span>
sample = 'AnythingElse'; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ERROR!</span></pre>
</div>
<p> 17 当输入参数是一个函数的时候,如果用ts约定。</p>
<div class="cnblogs_code">
<pre>const iTakeSomethingAndPassItAnErr = (x: (err: Error, data: any) => <span style="color: rgba(0, 0, 255, 1)">void</span>) =><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> 做一些其他的 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
};
iTakeSomethingAndPassItAnErr(() </span>=> <span style="color: rgba(0, 0, 255, 1)">null</span>); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ok</span>
iTakeSomethingAndPassItAnErr(err => <span style="color: rgba(0, 0, 255, 1)">null</span>); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ok</span>
iTakeSomethingAndPassItAnErr((err, data) => <span style="color: rgba(0, 0, 255, 1)">null</span>); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ok</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Error: 参数类型 `(err: any, data: any, more: any) => null` 不能赋值给参数类型 `(err: Error, data: any) => void`</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> iTakeSomethingAndPassItAnErr((err, data, more) => null);</span></pre>
</div>
<p> 18 使用mixins</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 所有 mixins 都需要</span>
type Constructor<T = {}> = <span style="color: rgba(0, 0, 255, 1)">new</span> (...args: any[]) =><span style="color: rgba(0, 0, 0, 1)"> T;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">///////////</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> mixins 例子</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">//////////</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 添加属性的混合例子</span>
<span style="color: rgba(0, 0, 255, 1)">function</span> TimesTamped<TBase extends Constructor><span style="color: rgba(0, 0, 0, 1)">(Base: TBase) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> class extends Base {
timestamp </span>=<span style="color: rgba(0, 0, 0, 1)"> Date.now();
};
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 添加属性和方法的混合例子</span>
<span style="color: rgba(0, 0, 255, 1)">function</span> Activatable<TBase extends Constructor><span style="color: rgba(0, 0, 0, 1)">(Base: TBase) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> class extends Base {
isActivated </span>= <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
activate() {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.isActivated = <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
}
deactivate() {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.isActivated = <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
}
};
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">/////////</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> 组合类</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">/////////</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 简答的类</span>
<span style="color: rgba(0, 0, 0, 1)">class User {
name </span>= ''<span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 添加 TimesTamped 的 User</span>
const TimestampedUser =<span style="color: rgba(0, 0, 0, 1)"> TimesTamped(User);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Tina TimesTamped 和 Activatable 的类</span>
const TimestampedActivatableUser =<span style="color: rgba(0, 0, 0, 1)"> TimesTamped(Activatable(User));
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">////////</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> 使用组合类</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">////////</span>
<span style="color: rgba(0, 0, 0, 1)">
const timestampedUserExample </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TimestampedUser();
console.log(timestampedUserExample.timestamp);
const timestampedActivatableUserExample </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TimestampedActivatableUser();
console.log(timestampedActivatableUserExample.timestamp);
console.log(timestampedActivatableUserExample.isActivated);</span></pre>
</div>
<p> </p>
<p>19 typescript中局部变量变为全局变量的方法。</p>
<div class="cnblogs_code">
<pre>const { called } = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> class {
count </span>= 0<span style="color: rgba(0, 0, 0, 1)">;
called </span>= () =><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.count++<span style="color: rgba(0, 0, 0, 1)">;
console.log(`Called : ${</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.count}`);
};
}();
called(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Called : 1</span>
called(); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Called : 2</span></pre>
</div>
<p> </p>
<p>20 tslint 对ts语法中不允许带数字,字符串,布尔声明后直接赋值。因为ts已经可以直接判断出类型了。</p>
<div class="cnblogs_code">
<pre>Type <span style="color: rgba(0, 0, 255, 1)">boolean</span> trivially inferred from a <span style="color: rgba(0, 0, 255, 1)">boolean</span> literal, remove type annotation</pre>
</div>
<p> </p>
<p>21</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">interface DataHandleFunctionType {
(param: []): [];
};
type DataHandleFunctionType </span>= (param: []) => [];</pre>
</div>
<p> </p>
<p> </p>
<p> 22 enum和接口的区别和联系</p>
<div>aa: CloudTypeEnum.Huawei = CloudTypeEnum.Huawei;</div>
<div>CloudTypeInterface.Huawei 接口这个使用只能代表字符串</div>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p></p>
<p></audio></p><br><br>
来源:https://www.cnblogs.com/coding4/p/11200430.html
頁:
[1]