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 var
s 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) => '');
A 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
The repository for high quality TypeScript type definitions的更多相关文章
- [TypeScript] Type Definitions and Modules
For example you are building your own module, the same as Lodash: my-lodash.d.ts declare module &quo ...
- (TODO:)下载图片,报错:warning: could not load any Objective-C class information from the dyld shared cache. This will significantly reduce the quality of type information available.
想使用NSInvocationOperation下载图片,然而并没有下载下来, NSData为nil, 还有报错:(打断点就报错) warning: could not load any Object ...
- Hex-Rays decompiler type definitions and convenience macros
/****************************************************************************************** Copyrigh ...
- [Typescript] Installing Promise Type Definitions Using the lib Built-In Types
To fix Promise is not recolized in TypeScript, we can choose to use a lib: npm i @types/es6-promise ...
- TypeScript Type Innference(类型推断)
在这一节,我们将介绍TypeScript中的类型推断.我们将会讨论类型推断需要在何处用到以及如何推断. 基础 在TypeScript中,在几个没有明确指定类型注释的地方将会使用类型推断来提供类型信息. ...
- TypeScript: type alias 与 interface
官方文档中有关于两者对比的信息,隐藏在 TypeScript Handbook 中,见 Interfaces vs. Type Aliases 部分. 但因为这一部分很久没更新了,所以其中描述的内容不 ...
- [TypeScript] Type check JavaScript files using JSDoc and Typescript 2.5
Typescript 2.5 adds JSDoc type assertion support for javascript file via ts-check service. First of ...
- TypeScript Type Compatibility(类型兼容)
TypeScript中的类型兼容是基于结构归类的.在普通分类的相比之下,结构归类是一种纯粹用于将其成员的类型进行关联的方法.思考下面的代码: interface Named { name: strin ...
- TypeScript type 类型别名
//6,类型别名 /**类型别名不能出现在声明右侧的任何地方. * 接口 vs. 类型别名 * 另一个重要区别是类型别名不能被extends和implements(自己也不能extends和imple ...
随机推荐
- Python 字符串处理大全.
Python 字符串 字符串是Pyhton中常用的数据类型,我们可以使用引号来创建字符串 . 创建字符串很简单 , 就不说了 . Python 访问字符串中的值 鬼叔本着简洁 使用的设计目的 , 在设 ...
- yii2 生成PDF格式的文件
1 .先把mpdf-development.zip解压的类文件夹放到vendor目录里面,重命名为mpdf 2 .在vendor/composer/autoload_namespaces.php里面添 ...
- python构建模拟模型——网站独立访问用户数量
背景:发现一个有趣的现象,即一些用户在每一月都仅仅访问网站一次,我们想要了解这些人数量的变化趋势. 建立数学模型:简化问题,根据瓮模型推导出公式(具体推导见<数据之魅>,有时间再补充... ...
- YII2.0中实现高级注册
如何在不修改逻辑代码的情况下完美解决以上三个问题?看了下面的教程,一目了然! 以高级版2.0.6为例,打开/frontend/models/SignupForm.php class SignupFor ...
- 一个比较完整的Inno Setup 安装脚本
一个比较完整的Inno Setup 安装脚本,增加了对ini文件设置的功能,一个安装包常用的功能都具备了. [Setup] ; 注: AppId的值为单独标识该应用程序. ; 不要为其他安装程序使用相 ...
- [转]BEHAVOUR TREE
自从开博以来,每天都会关心一下博客的访问情况,看到一些朋友的订阅或者访问,不胜欣喜,也促使我去写一些更好的博文,来和大家分享和交流,从访问 统计来看,有相当一部分是来自于搜索引擎的流量,关键字以“行为 ...
- 重学STM32---(六)DAC+DMA+TIM
这两天复习了DAC,DMA再加上把基本定时器TIM6和TIM7看了一下,打算写一个综合点的程序,,,就在网上找了一些关于DAC,DMA和定时器相关的程序,最终打算写了输出正弦波的程序... 由于没有示 ...
- 动画(Animation) 、 高级动画(Core Animation)
1 演示UIImage制作的动画 1.1 问题 UIImage动画是IOS提供的最基本的动画,通常用于制作一些小型的动画,本案例使用UIImage制作一个小狗跑动的动画,如图-1所示: 图-1 1.2 ...
- IOS的MVC
1 翻牌游戏 1.1 问题 根据苹果MVC设计模式的思想原则实现一个简单的翻牌游戏,功能如下: 1)界面上随机摆放12张背面朝上的纸牌,界面效果如图-1所示: 图- 1 2)点击纸牌可以使纸牌翻页,翻 ...
- CSS 实现:元素相对于文档水平垂直居中
[要求]:如何用 CSS 实现水平/垂直居中一个元素(相对于文档) <body> <div class="content"></div> < ...