Open
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));
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)));
Actual type:
export const FEATURES: readonly Readonly<unknown>[];
💻 Use Cases
Getting correct type inference for JavaScript code on the wild web.