Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ThisType cant work in the block scope of function #52849

Closed
rudyxu1102 opened this issue Feb 19, 2023 · 2 comments
Closed

ThisType cant work in the block scope of function #52849

rudyxu1102 opened this issue Feb 19, 2023 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@rudyxu1102
Copy link

rudyxu1102 commented Feb 19, 2023

Bug Report

πŸ”Ž Search Terms

ThisType

πŸ•— Version & Regression Information

  • This is a crash
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about ThisType

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

function defineComp<Bar, Data>(
  options: {
    bar: Bar,
    foo: () => Data
  } & ThisType<{ bar: Bar }>
): Bar
function defineComp(options: any) {
  return options.bar
}

// failed
const aaa = defineComp({
  foo() {
    console.log(this.bar) // no error
    return {
      count: this.bar() // throw error
    }
  },
  bar() {
    return '1'
  },
})

// work
const bbb = defineComp({
  bar() {
    return '1'
  },
  foo() {
    console.log(this.bar)
    return {
      count: this.bar() // it's work
    }
  },
})

πŸ™ Actual behavior

TS error: (property) bar: unknown in aaa

πŸ™‚ Expected behavior

no error

@rudyxu1102 rudyxu1102 changed the title ThisType cant work in block scope of function ThisType cant work in the block scope of function Feb 19, 2023
@MartinJohns
Copy link
Contributor

I believe this is essentially a duplicate of #45213.

When encountering the this.bar() call the compiler has no clue what bar is because it hasn't been defined yet.

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants