TypeScript内联静态类型保护
我有一个名为foo的函数,它接受一个参数bar,它的类型是MyType。MyType的某些实例没有定义TypeScript内联静态类型保护,typescript,Typescript,我有一个名为foo的函数,它接受一个参数bar,它的类型是MyType。MyType的某些实例没有定义content属性,但我知道,bar.name==“goodName”的实例确实定义了content属性。然而,TypeScript不知道这一点,并在上面的代码中抛出一个错误。如何以这种方式将bar.content表示为TypeScript的字符串: type MyType = { name: string, content: string | undefined } function
content
属性,但我知道,bar.name==“goodName”
的实例确实定义了content
属性。然而,TypeScript不知道这一点,并在上面的代码中抛出一个错误。如何以这种方式将bar.content表示为TypeScript的字符串:
type MyType = {
name: string,
content: string | undefined
}
function doThing(baz: string) {...}
function foo(bar: MyType) {
if(bar.name === "goodName") {
doThing(bar.content) //Error
}
}
TypeScript中是否存在这样的功能?我知道用户定义的函数类型保护,但我想知道在这样的特定情况下是否有更简单的方法(它只需在
foo
中使用)。您是否尝试过类似于此的可选属性,或者这不适合您的情况:
if(bar.name === "goodName") {
bar.content is string; //Hypothetical
doThing(bar.content); //No error; the desired outcome
}
您是否为可选属性尝试过类似的方法,或者这不适合您的情况:
if(bar.name === "goodName") {
bar.content is string; //Hypothetical
doThing(bar.content); //No error; the desired outcome
}
如果
name
的选项数量有限,则可以使用有区别的联合:
type MyType = {
name: string,
content?: string
}
function doThing(baz: string) {...}
function foo(bar: MyType) {
if(bar.name === "goodName") {
doThing(bar.content) //Error
}
}
否则,您可以使用自定义类型保护来更改钢筋的类型:
type MyTypeBadName = {
name: 'badName',
}
type MyTypeGoodName = {
name: 'goodName',
content: string
}
function doThing(baz: string) {}
function foo(bar: MyTypeBadName | MyTypeGoodName) {
if(bar.name === "goodName") {
doThing(bar.content) //ok
}
}
如果
name
的选项数量有限,则可以使用有区别的联合:
type MyType = {
name: string,
content?: string
}
function doThing(baz: string) {...}
function foo(bar: MyType) {
if(bar.name === "goodName") {
doThing(bar.content) //Error
}
}
否则,您可以使用自定义类型保护来更改钢筋的类型:
type MyTypeBadName = {
name: 'badName',
}
type MyTypeGoodName = {
name: 'goodName',
content: string
}
function doThing(baz: string) {}
function foo(bar: MyTypeBadName | MyTypeGoodName) {
if(bar.name === "goodName") {
doThing(bar.content) //ok
}
}
使用代码>。从文档中:
一个新的!在类型检查器无法得出结论的上下文中,post fix表达式运算符可用于断言其操作数为非null和非未定义。具体来说,操作x!生成x类型的值,并排除null和undefined
我认为这正是您的用例
type MyType = {
name: string,
content: string | undefined
}
type MyTypeGoodName = {
name: 'goodName',
content: string
}
function isGoodName(baz: MyType): baz is MyTypeGoodName {
return baz.name == "goodName"
}
function doThing(s: string) { }
function foo(bar: MyType) {
if(isGoodName(bar)) {
doThing(bar.content) //ok
}
}
使用代码>。从文档中:
一个新的!在类型检查器无法得出结论的上下文中,post fix表达式运算符可用于断言其操作数为非null和非未定义。具体来说,操作x!生成x类型的值,并排除null和undefined
我认为这正是您的用例
type MyType = {
name: string,
content: string | undefined
}
type MyTypeGoodName = {
name: 'goodName',
content: string
}
function isGoodName(baz: MyType): baz is MyTypeGoodName {
return baz.name == "goodName"
}
function doThing(s: string) { }
function foo(bar: MyType) {
if(isGoodName(bar)) {
doThing(bar.content) //ok
}
}
TypeScript的区分联合需要信任成员具有文字类型。将name
声明为string
将不允许您执行您想要的操作。请参阅:TypeScript的区分联合需要信任成员具有文字类型。将name
声明为string
将不允许您执行您想要的操作。请参阅:我无法轻松访问MyType的定义,因此很遗憾,我无法使用它。我认为如果我这样做了,它仍然会引起问题,因为内容
与doThing
中的字符串连接,我无法轻松访问MyType的定义,因此很遗憾我无法使用它。我认为如果我这样做了,它仍然会引起问题,因为内容
在doThing