Giter VIP home page Giter VIP logo

Comments (4)

danielepolencic avatar danielepolencic commented on May 18, 2024 1

Gotcha. My thinking was more around function arguments:

function Hello(array: NonEmptyArray<string>) {
  /* body */
}

I tend to have a lot of guards in my code that do things like:

if (array.length === 0) {
  return
}

from type-fest.

BendingBender avatar BendingBender commented on May 18, 2024

Sounds very interesting and useful.

On the original discussion thread, they actually propose a different version of it with overloaded map function. The issue is that this type on its own has limited usefulness. Without an actual implementation for a type assertion function (<T>(array: T) => array is NonEmptyArray<T>) one would always end up with a plain array after mapping it.

So this seems a little out of scope for this project.

from type-fest.

BendingBender avatar BendingBender commented on May 18, 2024

Yeah, it just happened yesterday that I needed a type like this. The problem is ergonomics: Where do you get a value of this type from? Imagine an API like this:

function hello(array: NonEmptyArray<string>) {
  /* body */
}

Now you want to use it:

hello(['foo']); // => Works

const arr = ['foo'];
if (arr[0] != null) {
	hello(arr); // => Error
}
if (typeof arr[0] === 'string') {
	hello(arr); // => Error
}
if (arr.length > 0) {
	hello(arr); // => Error
}

So when you use it with a literal or tuple type, this will work fine. But as soon as you have a plain array type this will fail although we have an element or a length check. So there is actually limited purpose of this type alone. To preserve developer ergonomics you actually need to provide a type guard:

function isNonEmptyArray<T>(array: T): array is NonEmptyArray<T> {
	return arr[0] != null;
}

Otherwise every user of this type will have to re-implement this function. But it is currently outside of the scope of this project to provide imlementations like the one above.

from type-fest.

danielepolencic avatar danielepolencic commented on May 18, 2024

👍

from type-fest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.