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 ...
随机推荐
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- OD调试篇5--如何应对OD使用中的一些问题
打开小甲鱼给的进行恶搞过的程序,会发现一些问题 发现程序直接暂停,或者加载进来有问题. 那机智的我 通过对上一个没有恶搞过的exe可执行文件的PE头进行了比较 会发现其中的猫腻 那么我们去正常的修改一 ...
- Objective-C--- 多态 、 协议
1 编写交通工具程序 1.1 问题 本案例需要创建一个TRTransportation类,类中有一个方法叫print的方法,该方法默认输出 “显示交通工具信息”,这个类作为父类,派生出三个子类TRTa ...
- (基础篇)PHP与Web页面交互
PHP与Web页面交互是实现PHP网站与用户交互的重要手段.在PHP中提供了两种与Web页面交互的方法,一种是通过Web表单提交数据,另一种是通过URL参数传递. 这里我们将详细讲解表单的相关知识,为 ...
- Pythono 实现 Permutation
不管在R 还是python中,都有现成的函数来轻而易举地进行全排列(Permutation).无序排列等等.今天想要尝试一下使用自己写代码来实现全排列. 首先,我采用的算法如下: 对于一个数列 i.e ...
- UI学习笔记---第十天UITableView表视图编辑
UITableView表视图编辑 表视图编辑的使用场景 当我们需要手动添加或者删除某条数据到tableView中的时候,就可以使用tableView编辑.比如微信 扣扣中删除和某人的通话 当我们需要手 ...
- UI学习笔记---第一天
一.iOS概述 iOS是Apple公司的移动操作系统,主要⽤用于iPhone.iPad.iPad Mini.iPod Touch等移动产品. 借助iOS,我们可以开发视频类.美图类.新闻类.⾳乐类.团 ...
- Munin监控的安装与配置
Munin 是一款类似 RRD tool 的优秀系统监控工具,它能提供给你多方面的系统性能信息,例如 磁盘.网络.进程.系统和用户. Munin 的工作原理 Munin 以客户端-服务器模式运行,主服 ...
- ZSDR100 跑原材料MRP
*&---------------------------------------------------------------------**& Report ZSDR100*&a ...
- 快速对字符转义,避免跨站攻击XSS
XSS已经成为非常流行的网站攻击方式,为了安全起见,尽量避免用户的输入.可是有些情况下不仅不避免,反而要求鼓励输入,比如写博客.博客园开放性很高,可以运行手写的JS.之前比较著名的例子就是,凡是看到某 ...