方法链的回调中没有明确返回编译器错误地推断电流主题

发布于 2025-02-02 23:14:47 字数 1155 浏览 1 评论 0原文

在第一个示例中,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℡Ms空城旧梦 2025-02-09 23:14:47

的文档。然后()()

从回调函数返回的任何内容都将成为新主题,并将流入下一个命令(未定义除外)

此外,在回调函数中最后一个柏树命令的 如果没有返回,将作为新主题 作为新主题 而产生下一个命令。<<<<<<。 /p>

我不确定如何定义该行为(如果没有明确的返回,请切换主题)到打字稿编译器。

但是对我来说,这意味着主题可以具有类型任何

键入参数以删除错误消息

getNumber()
  .then(() => {
    getString()   // Typescript can't infer Subject will be string 
                  // just because there's no return
  })
  .then((returnedString: any) => {
    const myString: string = returnedString // no error
  })

From the docs for .then()

Whatever is returned from the callback function becomes the new subject and will flow into the next command (with the exception of undefined)

Additionally, the result of the last Cypress command in the callback function will be yielded as the new subject and flow into the next command if there is no return.

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

getNumber()
  .then(() => {
    getString()   // Typescript can't infer Subject will be string 
                  // just because there's no return
  })
  .then((returnedString: any) => {
    const myString: string = returnedString // no error
  })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文