microsoft / microsoft/TypeScript

Weak type not rejected when intersected with `ThisType`

Open
#56,995 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: This-Typing Help Wanted
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 Search Terms
  • function overload
  • ThisType
  • this
🕗 Version & Regression Information
  • This is a crash
⏯ Playground Link

https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgGIHt3IN4ChnJwBcyAzmFKAOa4C+uAJhAgDZxQowCuICYw6EMm4gADAAp2VEuICUyALwA+ZOPzIAZGkzqtAFQAWwUnoCeABwgAeDOiW5ZskgDd0wBo2ZsOwnnwFCIhJSMrrIAArs-HAsNpj2BPpGJhbWtvaOLm4euEHieAD0BcgAegD86sTIAOQAjNV0srk8EnKKKvm4RaUVBFV1DbSOXcUAAmCkALQQAB6WfNNQUOhQzWLibcqqhcXllSS1jU1AA

💻 Code
interface Foo {
  a: string
}
declare function fun0(arg: () => (
  & Foo
  & ThisType<Foo>
)): void
declare function fun0(arg: (
  & Partial<Foo>
  & ThisType<Foo>
)): void

fun0({
// ^? function fun0(arg: (Partial<Foo> & ThisType<Foo>)): void (+1 overload)
  a: '1'
})
fun0(() => ({
// ^? function fun0(arg: () => (Foo & ThisType<Foo>)): void (+1 overload)
  a: '1'
}))
// @ts-expect-error
fun0(() => ({
// ^? function fun0(arg: (Partial<Foo> & ThisType<Foo>)): void (+1 overload)
  a: 1
}))

When I intersect ThisType with a type similar to {} and use it as a function argument, it will always resolve to this declaration.

🙁 Actual behavior

overload to function fun0(arg: (Partial<Foo> & ThisType<Foo>)): void (+1 overload)

🙂 Expected behavior

overload to function fun0(arg: () => (Partial<Foo> & ThisType<Foo>)): void (+1 overload)

Additional information about the issue

When I comment ThisType intesection, it works.
image

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TypeScript Playground example and reproduce the overload-resolution behavior involving ThisType, Foo, and Partial. Trace the compiler's handling of intersections with ThisType and function arguments; done means the third call is rejected and the inferred overload matches the expected function type without a crash.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.