Skip to content

Unable to run Array.map on an Array #8890

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
xeoneux opened this issue May 31, 2016 · 4 comments
Closed

Unable to run Array.map on an Array #8890

xeoneux opened this issue May 31, 2016 · 4 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@xeoneux
Copy link

xeoneux commented May 31, 2016

TypeScript Version:

1.8

Code

function getARPTable() {
    return new Promise((resolve, reject) => {
        arp.table((error, devices) => {
            if (error) reject(error);
            else resolve(devices);
        });
    });
}

async function scanPorts() {
    let devices = await getARPTable();
    console.log(Array.isArray(devices)); // true
    devices = devices.map((device) => device); // error
                      ^
    return true;
}

scanPorts();

Expected behavior:

No error output

Actual behavior:

error TS2339: Property 'map' does not exist on type '{}'.

@kitsonk
Copy link
Contributor

kitsonk commented May 31, 2016

TypeScript cannot contextually determine what the Promise resolves to (see: #5254) therefore you have to assert it.

Something like:

function getARPTable() {
    return new Promise<any[]>((resolve, reject) => {
        arp.table((error, devices) => {
            if (error) reject(error);
            else resolve(devices);
        });
    });
}

@xeoneux
Copy link
Author

xeoneux commented May 31, 2016

@kitsonk Thanks! I created a similar interface to fix it. However, I am still not sure why does the error occur even after successful compilation to JavaScript and running under node?

@kitsonk
Copy link
Contributor

kitsonk commented May 31, 2016

Because, instead of inferring type any for the Promise resolution, TypeScript infers {} which is an object with no properties, therefore doesn't have a .map method. IIRC there was a lot of discussion in the ticket I linked to as to why {} instead of any makes sense when filling a generic that cannot be contextually determined. By default, TypeScript will still emit the code, even if it has errors. So while the run-time behaviour works, TypeScript couldn't be sure it would be when it emitted the code.

@xeoneux
Copy link
Author

xeoneux commented May 31, 2016

@kitsonk I didn't realise that the error was during compilation and not during runtime. Thanks for the clarification! 😄

@xeoneux xeoneux closed this as completed May 31, 2016
@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label May 31, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants