Skip to content

Add an overload to Object.freeze that preserves literal types #33767

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

Merged

Conversation

weswigham
Copy link
Member

This way when you write something like

const LogType = Object.freeze({
	error: "error", // message, c style arguments
	warn: "warn", // message, c style arguments
	info: "info", // message, c style arguments
	log: "log", // message, c style arguments
	debug: "debug", // message, c style arguments

	trace: "trace", // no arguments

	group: "group", // [label]
	groupCollapsed: "groupCollapsed", // [label]
	groupEnd: "groupEnd", // [label]

	profile: "profile", // [profileName]
	profileEnd: "profileEnd", // [profileName]

	time: "time", // name, time as [seconds, nanoseconds]

	clear: "clear", // no arguments
	status: "status" // message, arguments
});

(snippet from webpack) you get something with literally-typed members as a result, if possible. The intuition seems to be that it should just work like that already, but it does not currently.

@weswigham
Copy link
Member Author

@typescript-bot test this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Oct 22, 2019

Heya @weswigham, I've started to run the extended test suite on this PR at a5cb1a6. You can monitor the build here. It should now contribute to this PR's status checks.

@weswigham
Copy link
Member Author

@typescript-bot user test this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Oct 22, 2019

Heya @weswigham, I've started to run the parallelized community code test suite on this PR at a5cb1a6. You can monitor the build here. It should now contribute to this PR's status checks.

@typescript-bot
Copy link
Collaborator

The user suite test run you requested has finished and failed. I've opened a PR with the baseline diff from master.

@sandersn sandersn added the For Backlog Bug PRs that fix a backlog bug label Feb 1, 2020
@sandersn sandersn assigned sandersn and unassigned weswigham Mar 10, 2020
@typescript-bot typescript-bot added For Uncommitted Bug PR for untriaged, rejected, closed or missing bug and removed For Backlog Bug PRs that fix a backlog bug labels Mar 3, 2022
@sandersn
Copy link
Member

sandersn commented Mar 3, 2022

@typescript-bot user test this inline

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 3, 2022

Heya @sandersn, I've started to run the diff-based community code test suite on this PR at 89cfe35. You can monitor the build here.

Update: The results are in!

@typescript-bot
Copy link
Collaborator

@sandersn
Great news! no new errors were found between main..refs/pull/33767/merge

@sandersn
Copy link
Member

sandersn commented Mar 3, 2022

@weswigham this seems like a plausible change even though it's old. It didn't create any new errors in the user tests. Think it's worth trying out?

@weswigham
Copy link
Member Author

Yeah, I think it's still a reasonable change.

@weswigham weswigham merged commit ddad61e into microsoft:main Mar 3, 2022
@Andarist
Copy link
Contributor

Andarist commented Mar 4, 2022

Out of curiosity... if I hover over the call using this signature I might see something like this:

function freeze<{
    a: 1;
}, string | number | bigint | symbol>(o: {
    a: 1;
}): Readonly<{
    a: 1;
}>

In here, the U was not "computed" but instead its constraint is displayed. What kind of TS behavior/trait is at play here? On what foundation/principles this PR is built? 🤔

@weswigham
Copy link
Member Author

We assign positions literal types when they're contextually typed by a generic with literal bases in the constraint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants