Skip to content
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

Incorrect types on Array.filter(n => !!n) #41090

Closed
Fi1osof opened this issue Oct 13, 2020 · 2 comments
Closed

Incorrect types on Array.filter(n => !!n) #41090

Fi1osof opened this issue Oct 13, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@Fi1osof
Copy link

Fi1osof commented Oct 13, 2020

TypeScript Version: 4.0.2

Search Terms:
filter(), reduce()

Code

type MaybeValueArrayType = Array<number | undefined>;
type ValueArrayType = Array<number>;

const maybeValueArray: MaybeValueArrayType = [1,2,3, undefined];

const valueArrayType__ok = maybeValueArray.reduce((curr, next) => {
    if(next) {
        curr.push(next);
    }
    return curr;
}, [] as ValueArrayType);

const valueArrayType__OK_2: ValueArrayType = maybeValueArray as ValueArrayType;

// Realy OK???
const errorTypesArray: ValueArrayType = [
    1,2,3, 
    undefined, 
    // OK?
    null, 
    // OK too?
    {}
] as ValueArrayType;

// Why error with filter?
const valueArrayType__error: ValueArrayType = maybeValueArray.filter(n => !!n) ;

Expected behavior:

const valueArrayType__error: ValueArrayType = maybeValueArray.filter(n => !!n) ;

Should not return error due filter(n => !!n) returns only empty array or array with positive values, like via reduce example.

Actual behavior:

Type '(number | undefined)[]' is not assignable to type 'ValueArrayType'. Type 'number | undefined' is not assignable to type 'number'. Type 'undefined' is not assignable to type 'number'.

Playground Link:
https://www.typescriptlang.org/play?#code/C4TwDgpgBAsghiARhAanANgVwgQQE54IAq40AvFPoSADwB2mAtsnlAD5SZ0AmEAZgEs6EbgD4A3AChQkKGiy4CxUlApUE9JiwmTJAYwD2dAM7AojBMnnZ1IAFyxLqDDaUgSsigG0AjABoAJj8AZj9OHn4hEQBdKX0jUygANxdFag8IAH1MgwBrVXMnazSEADo8EUw9CAAKGr1MAjDhAA9gAEpVUSgAb0koAagBPhrWjt7+wamGglKwTGMAC1GINvapKYBfSYGK4Ea6KBm8KU2wr2ioOGM5VNsM9d1DEzMUhXvSbIB5AGlMgIcxQ+nkKSGc7zcVxuQLcGTiAHp4VAAEoQDAgKC-AD8OPiLygEAIBjwGWMtkBd1hKm8Oyg-iCoSgtK4vEEwm4YVpiMxPyxtIY6HQnKm3N+UGABgMfKmPW2l2utwh6VICKRAHVFhjCXhiVioAB3ATARZQQToYCEvnPRJvVzKyDZbXEilK5QgixgmHUUpmi14UZdKAAQiDdE6UiAA

@MartinJohns
Copy link
Contributor

MartinJohns commented Oct 14, 2020

Duplicate of #16069. Related: #38390.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Oct 14, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' 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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants