I'm composing a Redux selector with Ramda in TypeScript:
const getAvatar = pipe(
getCurrentUsersProfile,
propOr('/images/default-avatar.png', 'avatar'),
);
The tooltip for getAvatar
says it returns unknown
.
With the prop
function I can assert a return type like this:
const getCurrentUsersId = pipe(
getUserProfileSlice,
prop<'currentUsersId'>('currentUsersId'),
);
How can I get it to know that propOr
always returns a string
?
The pipe
function excepts a list of types. The 1st type is the input of the 1st function, and the other denote the return type of each function in order. The last type is the result of the last function, and the entire pipe.
type Store = {}; // type of your redux store
type UserProfile = {}; // type of the UserProfile slice
const getAvatar = pipe<Store, UserProfile, string>(
getCurrentUsersProfile,
propOr('/images/default-avatar.png', 'avatar'),
);
You can explicitly type the propOr
call, and add the return value (stackblitz):
import { pipe, propOr } from 'ramda';
const getCurrentUsersProfile = () => {};
const getAvatar = pipe(
getCurrentUsersProfile,
propOr('/images/default-avatar.png', 'avatar') as (src: any) => string,
);