问题描述
我的打字稿水平是'ABSOLUTE BEGINNER',但我有很好的OOP背景.我正在使用打字稿构建一个引用包含以下接口的外部t.ds
库的:
My level of typescript is 'ABSOLUTE BEGINNER' but I have a good OOP background. I am building an with typescript that reference an external t.ds
library that contains the following interface:
interface ISimpleObject {
foo: string;
bar?: any;
}
现在,如果要调用具有IRequestConfig参数的方法,该如何创建一个方法?我可以看到其他选项:
Now, if I want to call a method that has an IRequestConfig parameter, how do I create one? I can see different options:
- 创建ISimpleObject的简单实现.我不喜欢这种方法,因为它对我来说像样板代码
-
不要初始化对象(我担心这会破坏某些东西...):
- Create a simple implementation of ISimpleObject. I don't like this approach because it looks like boilerplate code to me
don't initialize the object (I fear this could break something...):
var x :IsimpleObject;x.bar = 'xxx';callMethod(x);
var x :IsimpleObject;x.bar = 'xxx';callMethod(x);
发布一个pojo:
var x :IsimpleObject = <IsimpleObject>{foo: 'yyy', bar:'xxx'};
我也不喜欢这种方法,因为它不强制执行类型安全...
I don't like this approach either because it doesn't enforce type safety...
我想这是一个相当琐碎的问题,我缺少有关打字稿的琐碎内容.
I guess this is a fairly trivial question and I am missing something trivial about typescript.
推荐答案
如果您的界面类似于:
interface ISimpleObject {
foo: string;
bar?: any;
}
此接口仅在编译时和代码提示/智能提示时使用.接口用于提供一种严格且类型安全的方式,以一致的方式使用带有已定义签名的对象.
This interface is only used at compile time and for code-hinting/intellisense. Interfaces are used to provide a rigorous and type-safe way of using an object with a defined signature in a consistent manner.
如果您具有使用上面定义的interface
的功能:
If you have a function using the interface
defined above:
function start(config: ISimpleObject):void {
}
如果对象没有ISimpleObject
接口的确切签名,则TypeScript编译将失败.
The TypeScript compile will fail if an object does not have the exact signature of the ISimpleObject
interface.
调用函数start
有多种有效方法:
There are multiple valid techniques for calling the function start
:
// matches the interface as there is a foo property
start({foo: 'hello'});
// Type assertion -- intellisense will "know" that this is an ISimpleObject
// but it's not necessary as shown above to assert the type
var x = <ISimpleObject> { foo: 'hello' };
start(x);
// the type was inferred by declaration of variable type
var x : ISimpleObject = { foo: 'hello' };
start(x);
// the signature matches ... intellisense won't treat the variable x
// as anything but an object with a property of foo.
var x = { foo: 'hello' };
start(x);
// and a class option:
class Simple implements ISimpleObject {
constructor (public foo: string, public bar?: any) {
// automatically creates properties for foo and bar
}
}
start(new Simple("hello"));
每当签名不匹配时,编译将失败:
Any time the signature doesn't match, the compile will fail:
// compile fail
var bad = { foobar: 'bad' };
start( bad );
// compile fail
var bad: ISimpleObject = { foobar: 'bad' };
// and so on.
没有正确"的方法来做到这一点.这是样式选择的问题.如果它是一个构造的对象(而不是直接作为参数传递),我通常会声明类型:
There is no "right" way to do it. It's a matter of style choice. If it were an object that was constructed (rather than just directly passed as a parameter), I'd normally declare the type:
var config: ISimpleObject = { foo: 'hello' };
这样,代码补全/IntelliSense可以在我使用config
变量的任何地方工作:
That way code-completion/IntelliSense will work anywhere I used the config
variable:
config.bar = { extra: '2014' };
TypeScript中没有广播".它被称为类型断言,在这里描述的情况下是不需要的(我在上面包括了可以使用它的示例).在这种情况下,无需声明变量Type,然后再使用断言(因为类型是已知的).
There is no "casting" in TypeScript. It is called a type assertion and shouldn't be needed in the cases described here (I included an example above where it could be used). There's no need to declare the variable Type and then use an assertion in this case (as the type was already known).
这篇关于打字稿介面初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!