Comments (4)
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.
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.
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.
👍
from type-fest.
Related Issues (20)
- How can I use the classic Omit functionality applied to an array type instead of a type? HOT 2
- PartialOnUndefinedDeep removes non-optional properties when the input type contains an index signature HOT 1
- [4.8.0] fails when there are no DOM types HOT 1
- [4.8.0] type error on jsonify HOT 5
- Audit all types for whether they need `readonly` modifier HOT 1
- "Stricter" intersection type? HOT 2
- `ConditionalSimplifyDeep` is not exported from root HOT 1
- Proposal: `PickNonNever<Type>`
- v4.8.0 break SnakeCasedPropertiesDeep with enum HOT 1
- "Writable" is imported from external module "./writable.js" but never used in
- `Paths` should not recurse into `Map`'s prototype properties. HOT 9
- ConditionalPickDeep does not work with deep interfaces
- `Addition` And `Subtraction` Type Proposal HOT 3
- `MergeDeep` removes optionals from `any` and `never` fields HOT 2
- Nested structure without index signature is not assignable to `Jsonifiable` HOT 1
- MergeDeep is never when compiled to .d.ts HOT 5
- ReadonlyDeep turns `void` into `unknown`
- Canary: `MergeDeep` failures in `typescript@next` not detected by canary tests HOT 3
- 4.9.0 fails with "Cannot find module './camel-case' or its corresponding type declarations" HOT 1
- IfType type guards HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from type-fest.