microsoft / microsoft/TypeScript

Add a flag to error when an uninitialized function is called

Open
#61,267 0 comments 0 reactions 0 assignees View on GitHub
Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

### 🔍 Search Terms

uninitialized function - uninitialized - uninitialized function call

### ✅ Viability Checklist

- [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- [x] This wouldn't change the runtime behavior of existing JavaScript code
- [x] This could be implemented without emitting different JS based on the types of the expressions
- [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- [x] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- [x] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

### ⭐ Suggestion

This is a request to add a new configuration flag that makes typescript error when an uninitialized **function** is called within the same scope

[This](https://github.com/microsoft/TypeScript/issues/56827#issuecomment-1863251126) is a comment made by @RyanCavanaugh on a similar topic (using an uninitialized variable) for why an error is not thrown by typescript in the case of using an uninitialized variable (not calling a function)

There are more comments on this topic

### But the same argument of why function parameters assignment is contra-variant applies to why throw an error when an uninitialized function is called. Typescript doesn't know if it will surely error but it prevents it anyway

### 📃 Motivating Example

The below is a case of a runtime error thrown by calling an uninitialized variable which typescript didn't prevent

https://www.typescriptlang.org/play/?#code/GYVwdgxgLglg9mABAMXBAjIgFKSAubASgF4A+ANzhgBNCBvAKEWcVwi0MQHovEoALGAGc+-AE5wA7iICGiMeFgBbAKaIVYiWKYsxKqCDFI65GQBsQKggCYAvg3sM2sBIgDm+gHIrJqSEURGFnl9QyQORDJECAQhODMVADozODcsAANvXzR0wgcGBh4YsCEoQNMLFVtIlDR0LDAfPwhOHnUADwAHFWgVakQAIxV+GXJ4MQIBYVEJaUQ5KABPbqEIMRhOso0tRE69chUwWDA3eUUYVXVNODEhBmb6jjI6REbsyAjbVt5wFS6eqB9QbDUbjSaCETUOAqIRgADkZQEs3mfGWMLWGy21zEiEkggg-EQCRk1BEUDgKIURwuam2NwKxVKryaaBqHigWWaHCAA

### 💻 Use Cases

1. What do you want to use this for? **Preventing runtime errors**
2. What shortcomings exist with current approaches? **none**
3. What workarounds are you using in the meantime?

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the motivating TypeScript Playground example and the linked discussion in issue #56827 about uninitialized variables. Define what the new configuration flag should diagnose for same-scope uninitialized function calls, and consider the stated constraints that it must not change emitted JavaScript or existing runtime behavior.

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.