admin管理员组文章数量:1391960
I have the following code
type SetupProps = {
defaults: string;
}
export class Setup extends React.Component<SetupProps, SetupState> {
constructor(props: any) {
super(props);
this.props.defaults = "Whatever";
}
When trying to run this code the TS piler returns the following error:
Cannot assign to 'defaults' because it is a constant or a read-only property.
How is deafualts
a readonly property, when its clearly not marked this way.
I have the following code
type SetupProps = {
defaults: string;
}
export class Setup extends React.Component<SetupProps, SetupState> {
constructor(props: any) {
super(props);
this.props.defaults = "Whatever";
}
When trying to run this code the TS piler returns the following error:
Cannot assign to 'defaults' because it is a constant or a read-only property.
How is deafualts
a readonly property, when its clearly not marked this way.
1 Answer
Reset to default 1You're extending React.Component
and it defines props
as Readonly<SetupProps>
class Component<P, S> {
constructor(props: P, context?: any);
...
props: Readonly<{ children?: ReactNode }> & Readonly<P>;
state: Readonly<S>;
...
}
Source
If you want to assign some default values, you can go with something like:
constructor({ defaults = 'Whatever' }: Partial<SetupProps>) {
super({defaults});
}
本文标签:
版权声明:本文标题:javascript - Typescript "Cannot assign to property, because it is a constant or read-only" even though propert 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744691494a2620022.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论