下面进行例子部分。对于每个例子,先是使用使用方法,然后是类型声明。 如果有多个好的声明表示方法,会列出多个。
animalFactory.create("dog");
animalFactory.create("giraffe", { name: "ronald" });
animalFactory.create("panda", { name: "bob", height: 400 });
// Invalid: name must be provided if options is given
animalFactory.create("cat", { height: 32 });
namespace animalFactory {
intece AnimalOptions {
name: string;
height?: number;
weight?: number;
}
function create(name: string, animalOptions?: AnimalOptions): Animal;
}
zooKeeper.workSchedule = "morning";
zooKeeper(giraffeCage);
// Note: Function must precede namespace
function zooKeeper(cage: AnimalCage);
namespace zooKeeper {
var workSchedule: string;
}
var w = widget(32, 16);
var y = new widget("sprocket");
// w and y are both widgets
w.sprock();
y.sprock();
intece Widget {
sprock(): void;
}
intece WidgetFactory {
new(name: string): Widget;
(width: number, height: number): Widget;
}
declare var widget: WidgetFactory;
// Either
import x = require('zoo');
x.open();
// or
zoo.open();
declare namespace zoo {
function open(): void;
}
declare module "zoo" {
export = zoo;
}
// Super-chainable library for eagles
import eagle = require('./eagle');
// Call directly
eagle('bald').fly();
// Invoke with new
var eddie = new eagle('Mille');
// Set properties
eddie.favorite = 'golden';
// Note: can use any name here, but has to be the same throughout this file
declare function eagle(name: string): eagle;
declare namespace eagle {
var favorite: string;
function fly(): void;
}
intece eagle {
new(awesomeness: number): eagle;
}
export = eagle;
addLater(3, 4, x => console.log('x = ' + x));
// Note: 'void' return type is preferred here
function addLater(x: number, y: number, (sum: number) => void): void;
如果你想看其它模式的实现方式,请在这里留言! 我们会尽可能地加到这里来。
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/bofangqi/article-22570-2.html
谢作诗这厮脑袋进水了吧
我会相信质监局买的是假货
更新9