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

Array.some should reflect that the array is not empty #60103

Open
matthieusieben opened this issue Sep 30, 2024 · 1 comment
Open

Array.some should reflect that the array is not empty #60103

matthieusieben opened this issue Sep 30, 2024 · 1 comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@matthieusieben
Copy link

matthieusieben commented Sep 30, 2024

⚙ Compilation target

es5

⚙ Library

es5

Missing / Incorrect Definition

some in lib.es5.d.ts could be defined as follows (return type from boolean to this is [T, ...T[]]):

interface Array<T> {
    some(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): this is [T, ...T[]];
}

Sample Code

playground

function requiresOneItem<T>(array: [T, ...T[]]) {}
function isFoo<T>(item: T) { return true }

const array = [1, 2]

if (array.some(isFoo)) {
  requiresOneItem(array) // Argument of type 'number[]' is not assignable to parameter of type '[number, ...number[]]'.
}


interface Array<T> {
    // Uncomment to test:
    // some(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): this is [T, ...T[]];
}

Documentation Link

N.A. Calling .some() on an array can only ever return true if the array is not empty.

@RyanCavanaugh
Copy link
Member

Type guards must always return true if the object matches the specified type.

function requiresOneItem<T>(array: [T, ...T[]]) {}

function isEven(item: number) {
  return item % 2 == 0;
}

const array: [number, ...number[]] = [1, 2]

if (array.some(isEven)) {
  requiresOneItem(array)
} else {
  const n = array;
  // n: never, which is wrong
}

interface Array<T> {
    // Uncomment to test:
    some(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): this is [T, ...T[]];
}

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Sep 30, 2024
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

2 participants