I am looking to utilize sets as they are implemented in MDN with TypeScript. I have the most recent version of TypeScript installed in my package:
"typescript": "^5.4.5"
and my tsconfig includes:
"lib": ["dom", "es2022"],
However, when I write:
const set1:Set<number> = new Set([1,2])
const set2:Set<number> = new Set([1,2,3])
set1.isSubsetOf(set2)
I encounter the error:
Property 'isSubsetOf' does not exist on type 'Set<number>'.ts(2339)
I attempted to define my own 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;
intersection(value: Set<T>): this;
isSubsetOf(value: Set<T>): this;
readonly size: number;
}
This approach works when the definition is in the same file, but importing it from another file results in the same error. Is there a way to override the default definition for Sets in TypeScript?
My tsconfig:
{
"include": ["./src/**/*"],
"compilerOptions": {
"strict": true,
"esModuleInterop": true,
"lib": ["dom", "es2022"],
"jsx": "react-jsx"
}
}
My package.json:
{
"name": "react-typescript",
"version": "1.0.0",
"description": "React and TypeScript example starter project",
"keywords": [
"typescript",
"react",
"starter"
],
"main": "src/index.tsx",
"dependencies": {
"loader-utils": "3.2.1",
"react": "18.3.1",
"react-dom": "18.3.1",
"react-scripts": "5.0.1"
},
"devDependencies": {
"@types/react": "18.3.3",
"@types/react-dom": "18.3.0",
"typescript": "^5.4.5"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}