0

I have the following function:

 const UseFeature = ({ name }: { name: string }) => {
  const features = useContext(FeatureFlags);


   if (features === null) {
   throw new Error('You must wrap your components in a FeatureProvider.');
  }
  return Array.isArray(features) ? features.includes(name) : features[name];
 };

 export default UseFeature;

I call this function on another component :

const hasV1 = UseFeature('v1');

I get this error which I don't understand

Argument of type 'string' is not assignable to parameter of type '{ name: string; }'.  
 TS2345

Yet 'V1' is indeed a string element.

Have you ever had this problem ?

3 Answers 3

1

V1 is a string but UseFeature wants a { name: string; }

You have two options.

You could change the type when you call UseFeature

const hasV1 = UseFeature({name: 'v1'});

or change the declaration of UseFeature to

const UseFeature = (name: string ) => {

And still call it with const hasV1 = UseFeature('v1');

In this second example you are no longer destructuring an object. With the function using only one property / parameter I prefer the second solution more.

Sign up to request clarification or add additional context in comments.

Comments

0

I think it's because UseFeature destructures name, so is expecting that when you call the function. You've just passed a string:

const hasV1 = UseFeature('v1');

It is expecting an object:

const hasV1 = UseFeature({name: 'v1'});

Comments

0

this worked for me

const data:any= { name:'v1' }
const hasV1 = UseFeature(data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.