microsoft / microsoft/TypeScript

useUnknownForAnyReturn and useUnknownForAnyParam

Open
#55,108 3 comments 2 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

⭐ Suggestion

Currently there's useUnknownInCatchVariables which allows changing the error type in catch to unknown (from any) by default.
Using the same reasoning why that option was introduced, I think it would make sense for 1 or 2 more options in a similar vein:

  • useUnknownForAnyReturn: if a function returns "any", treat it as if it were "unknown" when the function is called. This is useful especially with 3rd party libs, but also in general, since "unknown" is much stricter than "any" (which is one of the reasons e.g. @typescript-eslint has a "no-unsafe-assignment" rule - which often cannot be fixed, since the "any" comes from a third party package)

  • useUnknownForAnyParamCallback: if a function/callback does not have any types specified, the default is any. It would make checking better if it would be treated as "unknown" instead of "any", when this flag is set.

🔍 Search Terms

All mentioned in the suggestion above

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

📃 Motivating Example

import foo from 'foo';

const bar = foo(); // foo returns any

acceptsStringOnly( arg ); // no error reported, since "any" is allowed for string, but when running the code in browser, this will give an error, that tsc could easily catch, if it were "unknown" instead of "any" 

if ( arg === false ) {
    // arg is still type "any", because type guard doesn't work on "any"
}

💻 Use Cases

Stricter type checking to avoid bugs popping up when using 3rd party code/any return, where that value is used in functions that have stricter requirements.
If you look at error data (e.g. sentry) you will see that this is actually the most common error reported from users browsers, as the "any" is an escape hatch from the type system, making type validation in typescript useless in some cases - which is the reason why the useUnknownInCatchVariables was implemented already.

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

Use the motivating example and the proposed useUnknownForAnyReturn and useUnknownForAnyParamCallback semantics as the starting specification; no source file or test is named in the issue. Clarify whether one or both options are desired, then define the type-checking behavior and tests that would demonstrate completion.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.