当前位置:首页 > 服务端 > The repository for high quality TypeScript type definitions

The repository for high quality TypeScript type definitions

Best practices

This is a guide to the best practices to follow when creating typing files. There are a variety of different ways that typing files can be constructed. Different approaches can be used - this is intended as a guide to what approaches make sense in certain scenarios.

Also, it's a steer on how to deal with limitations in TypeScript. As much as it hurts to say it, TypeScript v1.0 is not flawless. There are certain minor flaws / shortcomings in the language which have implicatations for how typings are created. Here we will detail those limitations, how they can be worked around at present and how you can best vote for improvements in the language on the TypeScript site.

Ghost modules

Also called non-instantiated modules. Instead of polluting the global namespace with many interfaces, it is okay to create a module that contains interfaces only. This does not introduce a variable on the global namespace (see safety in below sample) and this module can only be used for types.

// this pattern has 3 name in top level
interface NodeFoo { }
interface NodeBar { } interface NodeBuzz { } // this ghost module has 1 name in top level declare module NodeJS { interface Foo { } interface Bar { } interface Buzz { } } // safety! var n = NodeJS; // TS Error : Could not find symbol NodeJS 

This also allows you to open up further customization in external modules as interfaces declared inside external module declarations cannot be extended e.g. the following is good as people can customize foo further in other library definitions.

// Usage when declaring an external module
declare module 'foo' {
    var foo: NodeJS.Foo; export = foo; } 

Extending built-in types

There isn't a way to add static members to native objects at the moment as lib.d.ts defines them as a var Date:{/*members*/} and vars are not extendable. Two solutions are proposed to the TS team. Either use interfaces instead of var in lib.d.ts (vote) and/or make variables/classes open ended (vote)

For adding members to instances of native types there are relevant interfaces in available in lib.d.ts e.g.

// add members to Date instances
interface Date {
    newMember: number;
}

// usage var foo = new Date(); foo.newMember = 123; // okay 

Getter / Setter

Instead of :

declare function duration(value?: number): any; 

better to do:

declare function duration(): number; declare function duration(value: number): void; 

Fluent

Pretty self explanatory:

interface Something {
   foo(): Something;
   bar(): Something;
}

Callback signatures

Do not mark callback arguments as optional if they are passed in everytime by the calling code. Also leave the return as any if the calling code doesn't care. For example in the following good declaration foo is the calling code we are declaring that always calls with bar and bas and doesn't care of the callback return value:

declare function foo(callback: (bar: any, bas: any) => any): void;

// Usage is as expected by a JavaScript developer
foo(() => { });
foo((bar) => 123);
foo((bar, bas) => '');

wrong way to model it would be as shown below as it enforces restrictions the original calling code doesn't impose:

declare function foo(callback: (bar?: any, bas?: any) => void);

Function Overloading

A Union Type (any for now) is needed only for config object bags. For functions / constructors use function overloading e.g.

declare class Foo {
    constructor(foo: number);
    constructor(foo: string);
}

new Foo(123); // okay new Foo('123'); // okay new Foo(true); // Error 

Overload Ordering

Code with overloads must be manually sorted from the tightest/more-specific overload to loosest. See example below:

interface Parent { x; }
interface Child extends Parent { y; } function foo(p: Child): Child; function foo(p: Parent): Parent; function foo(p: any): any; function foo(p: any) { return p; } var a = foo({ x: 3, y: 4 }); // a: Child var b = foo({ x: 5 }); // b: Parent var y: any; var c = foo(y); // c: any

来源链接:https://www.cnblogs.com/rongfengliang/p/5228602.html

版权声明:
1、Java侠(https://www.javaxia.com)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaxia.com/server/124892.html

分享给朋友:

“The repository for high quality TypeScript type definitions” 的相关文章

软件工程复习要点2022年05月17日 14:38:01
今天,我有话说!2022年05月18日 23:15:30
关于设计模式2022年05月23日 23:02:13
第一章导言的笔记与思考2022年05月30日 22:43:38
Go语言为并发而生2022年05月30日 22:50:08
OpenHarmony的多内核2022年05月30日 23:18:14
深入浅出: 大小端模式2022年06月03日 23:10:37
.NET 开发框架技术资料搜集2022年06月04日 11:13:50