Skip to content

Instantly share code, notes, and snippets.

@MariusBongarts
Created August 4, 2023 06:49
Show Gist options
  • Save MariusBongarts/63232f23cfb956ef2561f40867217e4f to your computer and use it in GitHub Desktop.
Save MariusBongarts/63232f23cfb956ef2561f40867217e4f to your computer and use it in GitHub Desktop.
interface Context {
name: string;
metadata: Record<PropertyKey, unknown>;
}
function setMetadata(_target: any, context: Context) {
context.metadata[context.name] = true;
}
class SomeClass {
@setMetadata
foo = 123;
@setMetadata
accessor bar = "hello!";
@setMetadata
baz() { }
}
const ourMetadata = SomeClass[Symbol.metadata];
console.log(JSON.stringify(ourMetadata));
// { "bar": true, "baz": true, "foo": true }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment