Skip to content

Instantly share code, notes, and snippets.

Avatar

Ján Jakub Naništa janjakubnanista

View GitHub Profile
View articles.002.012.other-types.ts
const buttonTypeKeys = propertiesOf<typeof MyEnum>();
const stringProperties = propertiesOf<string>();
const promiseProperties = propertiesOf<Promise<unknown>>();
View articles.002.011.advanced.ts
// This will list all the readonly properties of MyInterface
const readonlyProperties = propertiesOf<MyInterface>({ readonly: true });
// This will list all the optional properties of MyInterface
const optionalProperties = propertiesOf<MyInterface>({ optional: true });
// This will list all the required properties of MyInterface
const requiredProperties = propertiesOf<MyInterface>({ optional: false });
// But feel free to ask for private readonly OR public optional properties
View articles.002.010.properties-of-some-payload.ts
import { propertiesOf } from 'ts-reflection';
// FREEDOM!!!
const payloadKeys = propertiesOf<SomePayload>();
View articles.002.009.values-of-button-types.ts
import { valuesOf } from 'ts-reflection';
// OMG SO MUCH BETTER
const buttonTypes: ButtonType[] = valuesOf<ButtonType>();
View articles.002.007.enum-as-const-console-log.ts
console.log(buttonTypes);
// The output does not look like what we'd expect!
['primary', 'PRIMARY', 'secondary', 'SECONDARY', 'link', 'LINK']
View articles.002.006.enum-as-const.ts
enum ButtonType {
PRIMARY = 'primary',
SECONDARY = 'secondary',
  LINK = 'link'
}
const buttonTypes: ButtonType[] = Object.values(ButtonType) as ButtonType[];
View articles.002.005.new-button-type.ts
// Our new error type ↴
type ButtonType = 'primary' | 'secondary' | 'error' | 'link';
// This compiles fine even though we now have a new ButtonType
const buttonTypes: ButtonType[] = ['primary', 'secondary', 'link'];
View articles.002.004.button-types-deleted.ts
// ButtonType 'link' is no longer in the array
const buttonTypes: ButtonType[] = ['primary', 'secondary'];
View articles.002.003.button-types.ts
const buttonTypes: ButtonType[] = ['primary', 'secondary', 'link'];
You can’t perform that action at this time.