Skip to content

A TypeScript custom transformer which enables to obtain keys of given type

License

Notifications You must be signed in to change notification settings

sasial-dev/rbxts-transformer-keys

 
 

Repository files navigation

rbxts-transformer-keys

A roblox-ts transformer which enables to obtain keys of given type.

Forked from https://github.com/kimamula/ts-transformer-keys

Requirement

TypeScript >= 3.4.1

How to use this package

This package exports 2 functions. One is $keys which is used in TypeScript codes to obtain keys of given type, while the other is a TypeScript custom transformer which is used to compile the $keys function correctly.

How to use $keys

import { $keys } from 'rbxts-transformer-keys';

interface Props {
  id: string;
  name: string;
  age: number;
}
const keysOfProps = $keys<Props>();

console.log(keysOfProps); // ['id', 'name', 'age']

How to use the custom transformer

// tsconfig.json
{
  "compilerOptions": {
    // ...
    "plugins": [
      { "transform": "rbxts-transformer-keys" }
    ]
  },
  // ...
}

Notes

  • The $keys function can only be used as a call expression. Writing something like $keys.toString() results in a runtime error.
  • $keys does not work with type generics, i.e., $keys<T>() in the following code is converted to an empty array([]).
class MyClass<T extends object> {
  keys() {
    // returns [] as generics do not work
    return $keys<T>();
  }
}

License

MIT

About

A TypeScript custom transformer which enables to obtain keys of given type

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 100.0%