microsoft / microsoft/TypeScript

Correctly handle generic functions (e.g.: `Object.freeze`) passed as callbacks to generic functions (e.g.: `Array.prototype.map`)

Open
#42,862 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Has Repro In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Suggestion

🔍 Search Terms

  • array map generic callback
  • object.freeze array map

✅ 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.

⭐ Suggestion

A generic transforming function (e.g.: Object.freeze) passed as a callback to a generic mapping function (such as Array.prototype.map), should be handled similarly to an arrow function with inferred types.

📃 Motivating Example

From engine262/src/engine.mjs:

// @target: ES2015
// @filename: index.ts
export const FEATURES = Object.freeze([
//                 ^?
	{
		name: 'Top-Level Await',
		flag: 'top-level-await',
		url: 'https://github.com/tc39/proposal-top-level-await',
	},
	{
		name: 'Hashbang Grammar',
		flag: 'hashbang',
		url: 'https://github.com/tc39/proposal-hashbang',
	},
	{
		name: 'RegExp Match Indices',
		flag: 'regexp-match-indices',
		url: 'https://github.com/tc39/proposal-regexp-match-indices',
	},
	{
		name: 'FinalizationRegistry.prototype.cleanupSome',
		flag: 'cleanup-some',
		url: 'https://github.com/tc39/proposal-cleanup-some',
	},
	{
		name: 'Arbitrary Module Namespace Names',
		flag: 'arbitrary-module-namespace-names',
		url: 'https://github.com/tc39/ecma262/pull/2154',
	},
	{
		name: 'At Method',
		flag: 'at-method',
		url: 'https://github.com/tc39/proposal-item-method',
	},
].map(Object.freeze));

Workbench Repro

Expected type:
export const FEATURES: readonly {
	readonly name: string;
	readonly flag: string;
	readonly url: string;
}[];
Like when using an arrow function:
// @target: ES2015
// @filename: index.ts
export const ARROW_FEATURES = Object.freeze([
//                       ^?
	{
		name: 'Top-Level Await',
		flag: 'top-level-await',
		url: 'https://github.com/tc39/proposal-top-level-await',
	},
	{
		name: 'Hashbang Grammar',
		flag: 'hashbang',
		url: 'https://github.com/tc39/proposal-hashbang',
	},
	{
		name: 'RegExp Match Indices',
		flag: 'regexp-match-indices',
		url: 'https://github.com/tc39/proposal-regexp-match-indices',
	},
	{
		name: 'FinalizationRegistry.prototype.cleanupSome',
		flag: 'cleanup-some',
		url: 'https://github.com/tc39/proposal-cleanup-some',
	},
	{
		name: 'Arbitrary Module Namespace Names',
		flag: 'arbitrary-module-namespace-names',
		url: 'https://github.com/tc39/ecma262/pull/2154',
	},
	{
		name: 'At Method',
		flag: 'at-method',
		url: 'https://github.com/tc39/proposal-item-method',
	},
].map((feature) => Object.freeze(feature)));

Workbench Repro

Actual type:
export const FEATURES: readonly Readonly<unknown>[];

💻 Use Cases

Getting correct type inference for JavaScript code on the wild web.

Related issues

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 index.ts repro in the issue and compare the map(Object.freeze) result with the arrow-function example; engine.mjs is the motivating usage. Confirm the implementation produces the expected readonly object-array type instead of readonly Readonly[] for the generic callback case.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.