Skip to content

Instantly share code, notes, and snippets.

@gorshkov-leonid
Created August 17, 2022 21:04
Show Gist options
  • Save gorshkov-leonid/ed304442d83bb8ff8e0024f9c9d8d212 to your computer and use it in GitHub Desktop.
Save gorshkov-leonid/ed304442d83bb8ff8e0024f9c9d8d212 to your computer and use it in GitHub Desktop.
Match props in object (template-literal-types)
// todo use typescript 4.1 with https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html to filter keys in type by prefix automatically
//      example: https://stackoverflow.com/questions/53501721/typescript-exclude-property-key-when-starts-with-target-string
//      example: https://stackoverflow.com/questions/71811073/substrings-of-string-unions-using-template-literal-types  
type FilterStartingWith<Set, Needle extends string> = Set extends `${Needle}${infer _X}` ? Set : never
type FilteredKeys<Prefix extends string> = FilterStartingWith<keyof AccessResourcesNames, Prefix>
type FilteredResourceName<Prefix extends string> = Pick<AccessResourcesNames, FilteredKeys<Prefix>>
function pickResources<Prefix extends string>(...prefix: Prefix[]): FilteredResourceName<Prefix>{
     return accessResourcesNames as FilteredResourceName<Prefix>
};
const res = pickResources('saDrillDown', 'saScheduleExport');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment