Comments (6)
It's a known trick of Typescript, that is when a union is put in a contravariant place(like function parameter) in conditional type it will be turned into a intersection type. So boolean = true | false
is turned into true & false
aka never
There is one easy and tricky solution
type VoidableEventCallback<EventValue> = {
0: () => void
1: (e: EventValue) => void
}[EventValue extends void ? 0 : 1]
But @Brooooooklyn what is the purpose of this type in the first place? In Typescript a function with a void parameter is quite identical to a function with no parameter
declare function foo(a: void): void
foo()
foo(1) // error
from rxjs-hooks.
It's seem like a TypeScript
behavior.
Maybe you should let TypeScript
resolve the generic type params itself.
const [callback, flag] = useEventCallback(($events: Observable<React.MouseEvent<HTMLElement> | boolean>) => $events.pipe(mapTo(false)), false)
callback(true)
from rxjs-hooks.
Same result. It's the VoidableEventCallback
giving the wrong types.
from rxjs-hooks.
What's version of the TypeScript in your project?
I'm using [email protected]
and const [callback, flag] = useEventCallback(($events: Observable<React.MouseEvent<HTMLElement> | boolean>) => $events.pipe(mapTo(false)), false)
works fine.
from rxjs-hooks.
I'm using 3.5.2 too.
const [callback, flag] = useEventCallback(($events: Observable<React.MouseEvent<HTMLElement> | boolean>) => $events.pipe(mapTo(false)), false)
is fine. It's callback(true)
got the error.
Found this on the doc, might be the reason?
Conditional types in which the checked type is a naked type parameter are called distributive conditional types. Distributive conditional types are automatically distributed over union types during instantiation. For example, an instantiation of T extends U ? X : Y with the type argument A | B | C for T is resolved as (A extends U ? X : Y) | (B extends U ? X : Y) | (C extends U ? X : Y).
from rxjs-hooks.
Very insightful! Didn't know a trick like this. Wish you'd comment earlier so that I don't have to make my own wheels.
from rxjs-hooks.
Related Issues (20)
- useEventCallback is not working with react 18 HOT 3
- Shows "difficulty.easy" in SQL section instead of Easy,Medium and Hard
- 为什么我写了依赖,但是不管用呢,界面并不会更新啊 HOT 2
- Feature request: add support for error handling and tap option HOT 1
- `useObservable` state generic is incorrectly inferred HOT 1
- Question about useEventCallback. HOT 1
- Passing custom values into `useEventCallback` callback HOT 1
- Feature request: useObservable to return the instant value on Component initialize if Observable has sync value HOT 1
- This project is alive? HOT 4
- An in-range update of @types/lodash is breaking the build 🚨 HOT 7
- An in-range update of husky is breaking the build 🚨 HOT 5
- An in-range update of sinon is breaking the build 🚨 HOT 2
- An in-range update of webpack-cli is breaking the build 🚨 HOT 2
- An in-range update of ts-jest is breaking the build 🚨 HOT 1
- useEventCallback stop working with RJXS.ajax HOT 2
- An in-range update of tslint-react is breaking the build 🚨 HOT 1
- Bug: Emit same object will not trigger re-render HOT 3
- I cant calc the dom size HOT 4
- Respond to prop changes with side effect HOT 2
- Suggestion: use object for `input` instead of tuple HOT 4
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 rxjs-hooks.