microsoft / microsoft/TypeScript

Change return type of `Object` call signature to `unknown` (currently `any`)

Open
#57,379 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 Search Terms

ObjectConstructor, Object, assignable, function

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about 5.3.3
⏯ Playground Link

https://www.typescriptlang.org/play?#code/CYUwxgNghgTiAEAzArgOzAFwJYHtVJxwAowAjALniIEp4BeAPngGcMYtUBzaygNxyzAA3ACgRiQjSEB6afECg5PACiADwAO4DCGDw4zZBAyUQMGDhjjJNekwBEt6jLmLVGzNt0h9hyqhzwTMwsJYmtGeFQDCEdZBWV1TQ89AyMA03NLYgBvAF9qWJcE9x1knzSgzKIAZTYObic410SSrxTff0CMkKIAOWQAW1ITGOd4ty0W71TOi0qAeVIAK00R+EAZcjHmzynjdIsgA

💻 Code
declare function foo(cb: () => string): void;

foo();// ✅ Expected result: error
foo(() => "");// ✅ Expected result: no error
foo(() => null);// ✅ Expected result: error
foo({})// ✅ Expected result: error
foo(String);// ✅ Expected result: no error
foo(Number);// ✅ Expected result: error

foo(Object);// ❌ Expected result: error
🙁 Actual behavior

Object (ObjectContructor) is assignable to any function, because it has a callable signature with return type of any.

This lets the developers to use Object where only a properly typed function would be allowed, which can lead to unexpected runtime errors.

I know this is a pretty edge case, but in my opinion it's worth looking into.

🙂 Expected behavior

Object is not assignable to functions with different return type

Additional information about the issue

No response

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 the ObjectConstructor call signature described in the issue. Check how changing its return type from any to unknown affects assignability, then verify that Object is rejected for the shown string-returning callback while the other examples retain their expected results.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.