Comments (5)
give type parameter to O.getWithDefault.
like
O.getWithDefault<number>(0).
from ts-belt.
@mattaiod you're issue above is because O.getWithDefault
expects 0
to be an Option<number>
type but it is a literal integer, take a look at the signature for why the non-curried version works:
export declare function getWithDefault<A>(option: Option<A>, defaultValue: NonNullable<A>): A;
export declare function getWithDefault<A>(defaultValue: NonNullable<A>): (option: Option<A>) => A;
As you see, the function would explicitly type A
as 0
in the curried version whereas it would resolve to just number
in the other, because normally the curried value would be within the pipe already and that would define the type. Basically don't use the curried version outside of a pipe/flow sequence OR type it yourself.
Note, there are a lot of instances where you need to do this when using getWithDefault. For example, the following would be a type error because the default is never[]
pipe(O.fromNullable([1, 2, 3]), O.getWithDefault([]))
from ts-belt.
How about making the default type an extended type of the given T type when it's curried?
I just made a proof-of-concept version of this.
https://www.typescriptlang.org/play?strictNullChecks=false&jsx=0&ts=5.4.0-dev.20240121#code/JYWwDg9gTgLgBAbzMMBTANAeQL5wGZQQhwBEAAiBAEbAA2AngPQwDOAtFarTCQFAz00cTGBjAIAOwA8AFQB8cALxwZcAD5wJAV1q11cLRIAmqPMAmojvXo0YBjLVCioJ8UGFqoQLmAEMxkryoAB6QsHB2kizwAOaoMADqwDAAFgAipr468MqycgAUJnhZ3ABqvrRaqABcAHKStTq0vlSeeQCUigXB1SIB0vKdcrxwo3DBAPw9RSUw5ZWo1rZghJAslnDunt6u-uISQaHQ8JES0XBxicnpmdkATEpwsugAGnAhMC5GLHD1Eo26FpteSKP4A5qtVB5AozbLzKrVF5DfI9Pr7DpdEZjSbTW5lCpVawCIQASU+xGUJF8JH0JCoNLUJDsfBs9kczlcm3A2x8e0CyDQ+SxmAAdABlIiofJUkjtdCs0YAPQmWMuSVSGWK2WlzPaWNsyt4etZKwgaw2Wy8vP6vAFUuF4slUjJXgKMrlCrghtGauumtmdx1stZhvaQA
from ts-belt.
@JUSTIVE Yes, I'm familiar with this solution, but I'd like to know if there's a way to improve this function to have this default behavior.
from ts-belt.
Maybe the NoInfer
utility type from the Typescript 5.4.0 would be solution for this.
updated example above, seems just working as intended
from ts-belt.
Related Issues (20)
- Can you tell me how to handle multiple Option types? HOT 4
- A.dropWhile is completely broken HOT 1
- `A.take` does not return empty array when `n` greater than length of array (as documented) HOT 2
- foy for the building not working properly HOT 2
- Update benchmarks to latest package versions HOT 4
- Proposal: G.isFinite HOT 1
- suggestion: reboot with rescript core? HOT 3
- snippets, and linter
- Ts-belt is 4x slower because it uses the arguments object
- Why an Option type instead of `type` | undefined? HOT 4
- Using unions for a result HOT 11
- ts-belt usage example flagged by TypeScript HOT 2
- why not `Do Notation` and `Applicative`
- Misleading annotations for Array.range
- Is this library still being maintained? HOT 1
- 🚧 Announcement: the current state of ts-belt 🚧 HOT 2
- `AR.isOk` and `AR.isError` not defined
- `AR.toOption` for `undefined` promise resolutions resulting in `{ BS_PRIVATE_NESTED_SOME_NONE: 0 }` HOT 1
- O.Option's functions with null literal value behaves differ from with undefined literal values HOT 1
- Bad Typing Ok / Error HOT 6
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 ts-belt.