Skip to content

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

Closed
@JoshuaKGoldberg

Description

@JoshuaKGoldberg

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions