Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is type of Set in typescript?

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??

like image 285
안다희 Avatar asked Dec 03 '25 18:12

안다희


1 Answers

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.

like image 80
James Hay Avatar answered Dec 06 '25 08:12

James Hay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!