PropertyKey
What
PropertyKey is a built-in TypeScript type that represents all possible types that can be used as a key in a JavaScript object: string, number, and symbol.
How
You can use PropertyKey in an index signature to allow the most flexible key types:
type InfrastructureTags = {
[key: PropertyKey]: any;
};
const server: InfrastructureTags = {
name: "Prod",
1: 420,
[Symbol("id")]: "ABC"
};
Why
Symbols are unique and immutable values often used to add “hidden” or private-like properties to objects that won’t collide with string keys. PropertyKey ensures your types are compatible with any valid JavaScript property access pattern.
Summary
Use PropertyKey when building generic utilities or frameworks that must support the full range of JavaScript property naming capabilities.