1

So I've a little dilema here, I'm using react native together with Typescript.

Have @types/react-native installed which has an interface 'TextInputStatic' declared under node modules @types/react-native.

export interface TextInputStatic extends NativeMethodsMixin, TimerMixin, React.ComponentClass<TextInputProperties> {
    State: TextInputState;

    /**
     * Returns if the input is currently focused.
     */
    isFocused: () => boolean;

    /**
     * Removes all text from the input.
     */
    clear: () => void;
}

In my component I need to use custom props for TextInput but because of interfaces defined in node modules typescript compiler throws an error:

Type '{ prop1: { name: string; anonymous: boolean; }; prop2: "f"; }' has no properties in common with type 'IntrinsicAttributes & IntrinsicClassAttributes<Component<TextInputProperties, ComponentState, nev...'.

When used like this:

<TextInput prop1={{name: 'loginField', anonymous: false }} prop2='f'></TextInput>

I looked at merging/ extending interfaces but no luck so far. Anyone knows how can I extend or allow TextInput to have custom props?

What I've tried to do is created an index.d.ts file and then:

import * as Native from 'react-native';

interface InputType extends Native.TextInputStatic{
    prop1?: any;
}

But no luck

Thanks

4
  • Make own component which wraps TextInput? Commented Apr 6, 2018 at 6:21
  • I was hoping I'd be able to write an interface or type that extends TextInputStatic or something like that Commented Apr 6, 2018 at 6:27
  • typescriptlang.org/docs/handbook/declaration-merging.html Something along the lines of this Commented Apr 6, 2018 at 6:49
  • Are you importing TextInput in your file? Commented Apr 6, 2018 at 11:25

2 Answers 2

1

First, you have to get the type from the component itself. Like this:

type TextInputStaticType = React.ComponentProps<typeof TextInputStatic>

Then you can extend the interface or type from there.

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

Comments

0

TextInputProps useable from reac-native >0.72 package

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.