Skip to content

--noImplicitAny codefixes infer anonymous object types despite appropriate primitives #29321

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

Closed
JoshuaKGoldberg opened this issue Jan 9, 2019 · 0 comments · Fixed by #36015
Closed
Labels
Domain: Quick Fixes Editor-provided fixes, often called code actions. Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Milestone

Comments

@JoshuaKGoldberg
Copy link
Contributor

TypeScript Version: 3.3.0-dev.20190108

Search Terms: noimplicitany infer code fix codefix quick suggested object literal primitive

Code

function checkEnd(text) {
    text.length;
    text.indexOf("z");
    text.charAt(0);
}

Expected behavior:

--noImplicitAny suggested fix on the text parameter:

checkEnd(text: string) {

Actual behavior:

function checkEnds(text: { length: any; indexOf: (arg0: string) => void; charAt: (arg0: number) => void; }) {

Related Issues: #13243 (parent tracking quick fixes); #28991 for similar issues with interfaces/types

At what point can TypeScript be confident something is a string, number, array, etc.? In the above case of a parameter whose usages match up to exactly one built-in primitive (string here), can it be inferred to that primitive instead of a very long anonymous object type that happens to be a subset of the primitive's?

Proposal: If a --noImplicitAny fix would add in an anonymous object type that is a subset of exactly one built-in primitive type, such as number, string, or boolean, it should add that primitive type instead.

@weswigham weswigham added Suggestion An idea for TypeScript Domain: Quick Fixes Editor-provided fixes, often called code actions. In Discussion Not yet reached consensus Experience Enhancement Noncontroversial enhancements and removed In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Jan 9, 2019
@JoshuaKGoldberg JoshuaKGoldberg changed the title --noImplicitAny codefixes infer anonymous object types despite appropriate interfaces in (or out of) scope --noImplicitAny codefixes infer anonymous object types despite appropriate primitives Feb 26, 2019
@RyanCavanaugh RyanCavanaugh added the Suggestion An idea for TypeScript label Mar 7, 2019
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Aug 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Quick Fixes Editor-provided fixes, often called code actions. Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants