Skip to content

Auto-array typing not working in 4.4 #45188

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
DanielRosenwasser opened this issue Jul 26, 2021 · 3 comments
Closed

Auto-array typing not working in 4.4 #45188

DanielRosenwasser opened this issue Jul 26, 2021 · 3 comments
Assignees
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Jul 26, 2021

function unique(elems: string[]) {
    const arr = [];
    for (const e of elems) {
        if (!arr.some(x => x === e)) {
            arr.push(e);
        }
    }
    return arr;
}

Expected: The type of arr is string[].
Actual: An error under noImplicitAny.

Variable 'arr' implicitly has type 'any[]' in some locations where its type cannot be determined.

Playground Link

Originally found in #45117, reported by @mkubilayk.

@Kingwl
Copy link
Contributor

Kingwl commented Jul 27, 2021

Possible related with #40359 ?

@ahejlsberg
Copy link
Member

This is working as intended. We complain about arr having an implicit any[] type because it is possible for control to reach the arr.some(...) call before any elements have been added to the array. It's equivalent to what happens in this example:

const a = [];
if (!a.some(x => x === 'foo')) {  // Error, a implicitly has type any[]
    a.push('bar');
}

Now, you could argue that the implicit any types don't matter because when the array is empty the arrow function is never called. But that's only because we know the semantics of the some method. It's not a general assumption we can make.

@ahejlsberg ahejlsberg added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Bug A bug in TypeScript labels Aug 7, 2021
@ahejlsberg ahejlsberg removed this from the TypeScript 4.4.1 (RC) milestone Aug 7, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants