[英]Optional parameter of class in typescript
given following code:给出以下代码:
class ConnectionOptions
{
host: string = "localhost";
port: number = 5672;
username: string = "guest";
password: string = "guest";
}
class ConnectorClass
{
options: ConnectionOptions = new ConnectionOptions();
SetOptions(options: ConnectionOptions)
{
console.log(options);
this.options = options;
}
}
// Adopt singleton pattern
var Connector = (function () {
var instance : ConnectorClass;
function createInstance() {
return new ConnectorClass();
}
return {
I: function () : ConnectorClass {
if (!instance) {
instance = createInstance();
}
return instance;
}
};
})();
// TestApp
Connector.I().SetOptions({ 'host': "192.168.17.5" });
In the last line there is following typescript error:在最后一行有以下 typescript 错误:
index.ts:50:26 - error TS2345: Argument of type '{ host: string; }' is not assignable to parameter of type 'ConnectionOptions'.
Type '{ host: string; }' is missing the following properties from type 'ConnectionOptions': port, username, password
50 Connector.I().SetOptions({ 'host': "192.168.17.5" });
I understand why the error is thrown: TS is expecting 4 properties but I only want to set 1 of them, isnt this possible in Typescript?我明白为什么会抛出错误:TS 期望有 4 个属性,但我只想设置其中的 1 个,这在 Typescript 中不可能吗?
For your code, an interface seems more suitable than a class.对于您的代码,接口似乎比 class 更合适。 You have several classes implementing the interface if required.
如果需要,您有几个实现接口的类。 Then, if you want some of arguments to be optional, you may use the optional modifier (
?
):然后,如果您希望某些 arguments 是可选的,您可以使用可选修饰符 (
?
):
interface ConnectionOptions {
host?: string;
port?: number;
username?: string;
password?: string;
}
const DefaultConnectionOptions: ConnectionOptions = {
host: 'localhost',
port: 5672,
username: 'guest',
password: 'guest',
}
Now your SetOptions
method can look something like this:现在您的
SetOptions
方法可能如下所示:
options: ConnectionOptions = DefaultConnectionOptions;
SetOptions(options: ConnectionOptions) {
console.log(options);
this.options = { ...DefaultConnectionOptions, ...options };
}
Your call Connector.I().SetOptions({ 'host': "192.168.17.5" });
您的呼叫
Connector.I().SetOptions({ 'host': "192.168.17.5" });
should now work because no property is mandatory.现在应该可以工作,因为没有属性是强制性的。
It may be that you want all connection options to be mandatory, but when calling SetOptions
they are not mandatory.您可能希望所有连接选项都是强制性的,但在调用
SetOptions
时它们不是强制性的。 In that case do not use the optional modifier on the properties.在这种情况下,不要在属性上使用可选修饰符。 Change your
SetOptions
signature to use Partial<T>
instead:将您的
SetOptions
签名更改为使用Partial<T>
:
SetOptions(options: Partial<ConnectionOptions>) {
console.log(options);
this.options = { ...DefaultConnectionOptions, ...options };
}
The above will still force this.options
to have every single property, but the argument options
does not have to provide all properties.以上仍将强制
this.options
具有每个属性,但参数options
不必提供所有属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.