typescript 我怎么能在打字稿中返回一个可为空的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49874116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How could I return a nullable value in typescript
提问by jstolz
in an NPM module I use typescript
在 NPM 模块中,我使用打字稿
"devDependencies": {
"@types/node": "^8.0.0",
"typescript": "^2.8.1"
}
and I want to return a private nullable parameter using a public method. Please refer to the axample below. The error I see is
我想使用公共方法返回一个私有的可为空参数。请参考下面的例子。我看到的错误是
Property 'string1' has no initializer and is not definitely assigned in the constructor.
If I assign an undefinded in the constructor I got the error
如果我在构造函数中分配了一个 undefined 我得到了错误
[ts]
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'
How should I do this in typescript, I′m from the c# side :)
我应该如何在打字稿中做到这一点,我来自 c# 方面:)
export class HowToDoThis {
private string1?: string;
public constructor() {
//this.string1 = undefined;
}
public add2String1(content: string) {
this.string1 += content;
}
public getString1(): string {
return this.string1;
}
}
回答by tru7
You can define
你可以定义
private string1: string | undefined;
回答by Duncan
The error message you are getting Type 'string | undefined' is not assignable to type 'string'
isn't because you assigned this.string = undefined
in the constructor, it is because you defined getString1()
as returning string
and you didn't check that this.string1
was in fact a string.
您收到的错误消息Type 'string | undefined' is not assignable to type 'string'
不是因为您this.string = undefined
在构造函数中赋值,而是因为您定义getString1()
为返回string
并且您没有检查它this.string1
实际上是一个字符串。
You can either change getString1()
so it does in fact always return a string, or you could make it return string|undefined
, or much simpler you could just initialise string1
to an empty string and never have it undefined.
你可以改变getString1()
它实际上总是返回一个字符串,或者你可以让它返回string|undefined
,或者更简单的你可以初始化string1
为一个空字符串并且永远不会让它未定义。
So this works:
所以这有效:
export class HowToDoThis {
private string1?: string;
public constructor() {
this.string1 = undefined;
}
public add2String1(content: string) {
this.string1 += content;
}
public getString1(): string {
return this.string1 || "";
}
}
But this would be better if only because calling add2String1('foo')
won't give you the string 'undefinedfoo'
:
但这会更好,因为调用add2String1('foo')
不会给你字符串'undefinedfoo'
:
export class HowToDoThis {
private string1: string = "";
public add2String1(content: string) {
this.string1 += content;
}
public getString1(): string {
return this.string1;
}
}
And this is best of all (don't use getter functions in typescript, you can always create a get
property later if you need to do something when a property is accessed):
这是最好的(不要在打字稿中使用 getter 函数,get
如果您需要在访问属性时执行某些操作,您可以随时创建属性):
export class HowToDoThis {
public string1: string = "";
public add2String1(content: string) {
this.string1 += content;
}
}
回答by Mattias Almén
Not sure what you want to do? Why do you want it to be undefined? I don't think you want to concatenate content with "undefined".
不确定你想做什么?为什么你希望它是未定义的?我认为您不想将内容与“未定义”连接起来。
So use:
所以使用:
private string1 = "";
or
或者
private string1: string;
public constructor() {
this.string1 = "";
}