Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
5 tasks done
ExE-Boss opened this issue Feb 18, 2021 · 2 comments
Labels
Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@ExE-Boss
Copy link
Contributor

ExE-Boss commented Feb 18, 2021

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

@RyanCavanaugh RyanCavanaugh added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Feb 18, 2021
@typescript-bot typescript-bot added the Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros label Feb 19, 2021
@jsmart523
Copy link

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
Copy link
Collaborator

typescript-bot commented Apr 13, 2022

👋 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

⚠️ Assertions:

  • const FEATURES: readonly Readonly[]

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

⚠️ Assertions:

  • const FEATURES: readonly Readonly[]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants