question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question related to typings for additional otherProps using interface merging

See original GitHub issue

English speaker, translated the template to english.

The environment in which the problem occurs is:

  • antd Version: 2.5.2
  • TypeScript version: 2.1.4
  • Operating system and version: Windows 10
  • Browser and its version: Not related to browser

What did you do? Please provide as detailed a step as possible.

I’m trying to add additional props to an , such as required and form, in a TypeScript environment, for example:

import { Input } from "antd";

<antd.Input form={formClass} value={value} required={true} onChange={e => this.handleChange(e)} />

The results you expect are:

I expected to be able to extend the InputProps interface to include the additional props so the TypeScript could compile without errors.

The actual result is:

The following errors are thrown while compiling:

Property 'form' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Input> & { children?: ReactNode; } & InputProps'.at line 50 col 29
Property 'required' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Input> & { children?: ReactNode; } & InputProps'.at line 52 col 29

This seems logical to me as the InputProps (https://github.com/ant-design/ant-design/blob/master/components/input/Input.tsx#L35) does not provide the required and form props, yet it allows for additional props to be passed: https://github.com/ant-design/ant-design/blob/master/components/input/Input.tsx#L224.

A reproducible online demo

Coulnd’t provide a reproducible online demo as codepen does not seem to typecheck the typescript.

Tl;dr

Is there a way to extend the InputProps from the antd library so I can add additional props? Or is there a better way to achieve this (without interface merging perhaps)?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

8reactions
BANG88commented, Dec 13, 2016

And if you want extend the Input component,its not difficult:


import { Input } from "antd";

// input has no default export it's Props,
// if you want give it a try,you can import it directly.
import { InputProps } from "antd/lib/input/input";

// Your Input Props
export interface MyInputProps extends InputProps {
    required?: boolean;
    form?: any;
}
// Extends antd's Input
class MyInput extends React.Component<MyInputProps, any>{
    render() {
        return <Input {...this.props} />
    }
}
// Usage
<MyInput size="large" form="your form data can be any type" required={true} />

If you have any questions, please reopen this issue.

0reactions
serhii-levchenkocommented, Sep 27, 2017

@Silox thanks for your quick reply. I will do the same.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Declaration Merging - TypeScript
To merge the namespaces, type definitions from exported interfaces declared in each namespace are themselves merged, forming a single namespace with merged ......
Read more >
How To Use Interfaces in TypeScript | DigitalOcean
Interfaces in TypeScript can be re-opened; that is, multiple declarations of the same interface can be merged. This is useful when you want...
Read more >
TypeScript workaround for rest props in React - Stack Overflow
My original question must have been poorly worded, because this answer does not address it at all yet has the most upvotes. :-P...
Read more >
Useful Patterns by Use Case - React TypeScript Cheatsheets
Wrapping/Mirroring a HTML Element​. Usecase: you want to make a <Button> that takes all the normal props of <button> and does extra stuff ......
Read more >
What Is Interface Declaration Merging in TypeScript?
That's exactly the question we're going to answer in today's article! The article has been originally published here. Introduction. If you aren't already ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found