I'm encountering an issue with TypeScript's type inference when dynamically assigning properties to an object. In one piece of code, TypeScript infers the type correctly, while in another similar piece of code, it does not. Here are the two examples:
Works!!
interface Student {
id: number;
name: string;
college: string;
marks: number;
}
interface Group {
College: string;
noOfStudent: number;
}
const groupByCollege = (arr: Student[]): Group[] => {
const result: Group[] = [];
const obj = {};
for (const el of arr) {
if (obj[el.college] === undefined) {
obj[el.college] = 1;
} else {
obj[el.college] += 1;
}
}
for (const key in obj) {
const singleCollege: Group = {
College: key,
noOfStudent: obj[key],
};
result.push(singleCollege);
}
return result;
};
Doesn't Work
const obj = {};
obj["d"] = 1; // Error: Property 'd' does not exist on type '{}'
I am currently using the latest version of TypeScript 5.7.3. In the tsconfig.json file, I executed the command tsc --init without making any additional changes.
I'm encountering an issue with TypeScript's type inference when dynamically assigning properties to an object. In one piece of code, TypeScript infers the type correctly, while in another similar piece of code, it does not. Here are the two examples:
Works!!
interface Student {
id: number;
name: string;
college: string;
marks: number;
}
interface Group {
College: string;
noOfStudent: number;
}
const groupByCollege = (arr: Student[]): Group[] => {
const result: Group[] = [];
const obj = {};
for (const el of arr) {
if (obj[el.college] === undefined) {
obj[el.college] = 1;
} else {
obj[el.college] += 1;
}
}
for (const key in obj) {
const singleCollege: Group = {
College: key,
noOfStudent: obj[key],
};
result.push(singleCollege);
}
return result;
};
Doesn't Work
const obj = {};
obj["d"] = 1; // Error: Property 'd' does not exist on type '{}'
I am currently using the latest version of TypeScript 5.7.3. In the tsconfig.json file, I executed the command tsc --init without making any additional changes.
Share Improve this question edited Feb 5 at 4:53 Nilesh Gagiya asked Feb 4 at 17:27 Nilesh GagiyaNilesh Gagiya 311 silver badge2 bronze badges 3- Welcome to Stack Overflow! Please edit the code to be a minimal reproducible example that demonstrates your problem and only your problem when we paste it into our own IDEs. If I look at your code in the TypeScript Playground I see errors in both pieces of code having to do with disallowed indexing. Your "works" code and "doesn't work" code are both problems. Please resolve the errors you're not asking about, or edit to clarify, so the question doesn't get closed as needing details or clarity. – jcalz Commented Feb 4 at 17:47
- 2 In both of your examples typescript throws errors. In both cases you have defined an empty object and in both of your cases TS says property does not exist. – Scott Z Commented Feb 4 at 18:39
- Can you post your tsconfig please? – Bergi Commented Feb 4 at 20:35
1 Answer
Reset to default 0Both of your examples produce an error in an up to date TypeScript compiler. They both define an obj
variable with an inferred type of empty object. You’ll need to be explicit about what you intend to store in obj
.
You seem to intend to use it as a key/value dictionary, with keys being strings and values being numbers. If so, you could use the utility type Record<Keys, Type>
like this:
const obj:Record<string, number> = {};