方法链的回调中没有明确返回编译器错误地推断电流主题
在第一个示例中,Mystring有TS 2322错误,因为它认为返回的串是一个数字(不是)。在第二个示例中,如果我们明确返回getNumber(),则没有TS 2322错误。
getTring()返回其定义中的值。柏树将当前的主题视为在方法链的回调中上次返回或明确返回的当前主题,在这种情况下,
我如何在不使用“返回”关键字的情况下解决此错误?我想使用示例1的样式,但不想误报语法突出显示
describe('example with error', () => {
it('example with error', () => {
getNumber()
.then((returnedNumber) => {
getString()
})
.then((returnedString) => {
const myString: string = returnedString // <-- HERE: "Type 'number' is not assignable to type 'string'. ts(2322)"
expect(myString).to.equal('string') // true
})
})
it('example without error', () => {
getNumber()
.then((returnedNumber) => {
return getString() // <-- HERE: explicitly returning
})
.then((returnedString) => {
const myString: string = returnedString
expect(myString).to.equal('string') // true
})
})
})
function getNumber(): Cypress.Chainable<number> {
return cy.wrap(1)
}
function getString(): Cypress.Chainable<string> {
return cy.wrap('string')
}
In the first example, myString has ts 2322 error because it believes returnedString is a number (it is not). In the second example, if we explicitly return getNumber(), there is no ts 2322 error.
getString() returns a value within it's definition. Cypress will consider the current subject as the one who was last returned or explicitly returned in the callback of a method chain, which in this case is myString()
How can I address this error without using 'return' keyword? I would like to use the style of example 1 but don't want false positive syntax highlighting
describe('example with error', () => {
it('example with error', () => {
getNumber()
.then((returnedNumber) => {
getString()
})
.then((returnedString) => {
const myString: string = returnedString // <-- HERE: "Type 'number' is not assignable to type 'string'. ts(2322)"
expect(myString).to.equal('string') // true
})
})
it('example without error', () => {
getNumber()
.then((returnedNumber) => {
return getString() // <-- HERE: explicitly returning
})
.then((returnedString) => {
const myString: string = returnedString
expect(myString).to.equal('string') // true
})
})
})
function getNumber(): Cypress.Chainable<number> {
return cy.wrap(1)
}
function getString(): Cypress.Chainable<string> {
return cy.wrap('string')
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从
的文档。然后()()
我不确定如何定义该行为(如果没有明确的返回,请切换主题)到打字稿编译器。
但是对我来说,这意味着主题可以具有类型
任何
。键入参数以删除错误消息
From the docs for
.then()
I'm not sure how you can define that behaviour (switching subject if no explicit return) to the Typescript compiler.
But to me the implication is that Subject can have type
any
.Typing the parameter as such removes the error message