0
interface Iparent {
  a: string
}
interface Ichild extends parent {
a: '';
}
const x: child = {}

This throws a compiler error when using child How to provide a default value for a in child interface?

Edit:

Class parent actually looks like this:

class parent extends React.Component<Iparent> {}

class child extends React.Component<Ichild> {}

parent class should have a prop but child class should just have a default value for it i.e. ''

1
  • 1
    You can't. Interfaces don't exist at runtime. Commented Dec 12, 2020 at 19:37

1 Answer 1

1

There are a few mistakes:

  1. a: '' not do default value, it means that a can be only '' it is like animal: 'dog' | 'cat' may be only 'dog' or 'cat' value.
  2. Interfaces have no default values - they are used like less specified types. If you want have default value you should use class child:
class child implements parent{
    a = '';
}
const x: child = new child();

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

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.