Skip to content

Commit

Permalink
fix: 🐛 ensures name subset helper always has a unique name
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The `IconNameSubset` helper is now dynamically
generated based off of the `interfaceName` property. Any references to
this will need updating.

fix nivekcode#91
  • Loading branch information
andy-polhill committed Dec 2, 2020
1 parent eadc9af commit e2e6cdc
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
![Logo](https://raw.githubusercontent.com/kreuzerk/svg-to-ts/master/assets/logo.png)

<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->

[![All Contributors](https://img.shields.io/badge/all_contributors-9-orange.svg?style=flat-square)](#contributors-)

<!-- ALL-CONTRIBUTORS-BADGE:END -->

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
Expand Down Expand Up @@ -382,12 +384,12 @@ advantages:

## Can I use the icons to generate a type?

If you have a method that decides which icon should be returned its useful to add a return type. To do so you can take advantage of the `IconNameSubset` helper generated by `svg-to-ts`.
If you have a method that decides which icon should be returned its useful to add a return type. To do so you can take advantage of the name subset helper generated by `svg-to-ts`. The name of the helper will be dynamically generated depending upon the value provided for the `interfaceName` property. An `interfaceName` of `MyIcon` will generate a helper called `MyIconNameSubset` as shown in the following example.

```typescript
import {IconNameSubset, myIconSmile, myIconLaugh} from 'my-icon-lib';

type emojiIcons = IconNameSubset<[typeof myIconSmile, typeof myIconLaugh]>;
type emojiIcons = MyIconNameSubset<[typeof myIconSmile, typeof myIconLaugh]>;

// resulting type is equal to type = 'smile' | 'laugh';

Expand Down Expand Up @@ -422,6 +424,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d

<!-- markdownlint-enable -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
4 changes: 2 additions & 2 deletions src/lib/generators/code-snippet-generators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ describe('Generators', () => {
it('should generate the correct type helper statement', () => {
const interfaceName = 'MyIcons';
const expectedStatement = `
export type IconNameSubset<T extends Readonly<${interfaceName}[]>> = T[number]['name'];
export type ${interfaceName}NameSubset<T extends Readonly<${interfaceName}[]>> = T[number]['name'];
`;

const generatedStatement = generateTypeHelper(interfaceName);
Expand All @@ -170,7 +170,7 @@ describe('Generators', () => {

const expectedStatement = `
import {${interfaceName}} from './${iconsFolderName}/${modelFileName}';
export type IconNameSubset<T extends Readonly<${interfaceName}[]>> = T[number]['name'];
export type ${interfaceName}NameSubset<T extends Readonly<${interfaceName}[]>> = T[number]['name'];
`;

const generatedStatement = generateTypeHelperWithImport(interfaceName, iconsFolderName, modelFileName);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/generators/code-snippet-generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export const generateVariableName = (prefix: string, filenameWithoutEnding): str
};

export const generateTypeHelper = (interfaceName: string): string => `
export type IconNameSubset<T extends Readonly<${interfaceName}[]>> = T[number]['name'];
export type ${interfaceName}NameSubset<T extends Readonly<${interfaceName}[]>> = T[number]['name'];
`;

export const generateTypeHelperWithImport = (
Expand Down

0 comments on commit e2e6cdc

Please sign in to comment.