Skip to content

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

Open
@ExE-Boss

Description

@ExE-Boss

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Has ReproThis issue has compiler-backed repros: https://aka.ms/ts-reprosIn DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions