I'm trying to change code javascript to typescript In Home.tsx,
interface IState {
mySet: ??; // what is type of Set??
myNum: number;
myStr: string;
myArr: number[];
}
class Home extends Component<IProps, IState> {
constructor(props: IProps) {
super(props)
this.state = {
mySet: new Set([]);
}
}
}
what is type of Set??
By looking at the definition in typescript for set, you will find this interface:
interface Set<T> {
add(value: T): this;
clear(): void;
delete(value: T): boolean;
forEach(callbackfn: (value: T, value2: T, set: Set<T>) => void, thisArg?: any): void;
has(value: T): boolean;
readonly size: number;
}
The <T>
means that a Set
type will require a type parameter. You would specify this in your IState
object like so:
interface IState {
mySet: Set<number>; // type depends on what type you want to store obviously.
myNum: number;
myStr: string;
myArr: number[];
}
If you don't care about the types (you probably should if you're using typescript), then you can simply pass any
as the type parameter, and the set will operate as it would in standard javascript.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.