microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.03k stars 12.49k forks source link

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

Open ExE-Boss opened 3 years ago

ExE-Boss commented 3 years ago

Suggestion

🔍 Search Terms

✅ Viability Checklist

My suggestion meets these guidelines:

⭐ 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: ```ts repro // @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](https://www.typescriptlang.org/dev/bug-workbench/?target=2&lib=#code/PTAEAEBcEMCcHMCmkBcoCiBlATABgIwCsAUCBAGYCWANogHbQC2ialdAJogB4B0kAzsW4AHAPaxIoAMai6-SQEEASkoDyAdQD6AMXQKAKgFUlWUAF5QqgEYArRFMg9ysRIgBeiABQBtUmFABgUHBIaAAegD8xACQAN4x0QzMaADk+qLCALQAMogAbojUoAoA7tCUkCkANAnk1NDwqZAZmbQF1JnQZRXVCQCusNSpABaQkML8KCDwFcN9VjwyjMCQUgDMAJzAwrAZovzQHc1ZbYWd3ZU10QC+V-HRiUwsoCkAEtD8w1bQdPCgAOKwJiMOC9B51BojD5fH7wMHRAZDF6jcaTaazeaLUTLVabba7MQHDrDaHfX5g24xe6PZIvJSIeDoLjCUAAWWgq2GoAAkhxKFJEPx4RDGi8XEhmZkQZzMmx2PzBfDESMxhMpsAZpA5gslit1lsdnsiZlxSIpRypMNZXyBUKrpS4gkks8Uto2IdKG4OZRZPSZvJYABPHiG5qQQPCRCLWg-PrCTDYxDC+qilJSGN0OOZfiJpWDFWo9Wa7VYnH6-FGw6ZdOIWNZHPMCl3J1PVIKWBWCpAoNs0TsPq0UAAOSe-GE0AFw9HychLzgncg3cDUr7A8QmWdY4n683eaRKRRavRWsxutxW3sIOwADZsNsB9RgNgiAAWJtUlu0lIKSSs5DDPsZ1TDkpX-QCrgRfNkVVNENQxHVsT1PFDUJKsKkQRhQK1cCYkpABdHgQWETxPHIWtIAGRAAEpzAAPksWx7EcZxXA8UjyMoqiuIAbmIIA)

Actual type:

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

💻 Use Cases

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

Related issues

jsmart523 commented 3 years ago

Another example:

type Widget = {name:string};

const widgets: Widget[] = [
  {name: 'gear'},
  {name: 'bearing'},
  {name: 'axle'}
];

const workingReadonlyWidgets = Readonly<Widget>[] = widgets.map(
  w => Object.freeze(w)
);

const brokenReadonlyWidgets = Readonly<Widget>[] = widgets.map(
  Object.freeze
); // this line complains
typescript-bot commented 2 years ago

:wave: Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of the repro in the issue body running against the nightly TypeScript.


Issue body code block by @ExE-Boss

:warning: Assertions:

Historical Information
Version Reproduction Outputs
4.2.2, 4.3.2, 4.4.2, 4.5.2, 4.6.2

:warning: Assertions:

  • const FEATURES: readonly Readonly[]