Skip to content

Commit

Permalink
feat(set): add Set support as a type (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
wassy92x authored Feb 9, 2020
1 parent cc2f9e0 commit 446c090
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/transformer/descriptor/tsLibs/typecriptLibs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export function GetTypescriptTypeDescriptor(node: ts.TypeReferenceNode, scope: S
);
case(TypescriptLibsTypes.Map):
return ts.createNew(ts.createIdentifier('Map'), undefined, undefined);
case(TypescriptLibsTypes.Set):
return ts.createNew(ts.createIdentifier('Set'), undefined, undefined);
default:
return GetDescriptor(ts.createNode(ts.SyntaxKind.UndefinedKeyword), scope);
}
Expand Down
1 change: 1 addition & 0 deletions src/transformer/descriptor/tsLibs/typescriptLibsTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export enum TypescriptLibsTypes {
Function = 'Function',
Promise = 'Promise',
Map = 'Map',
Set = 'Set'
}

export const TypescriptLibsTypesFolder: string = 'node_modules/typescript/lib';
20 changes: 20 additions & 0 deletions test/transformer/descriptor/tsLibs/tsLibs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,26 @@ describe('typescript lib', () => {
expect(anInterface.map.constructor).toBe(realMap.constructor);
});

it('should create a new Set for a Set', () => {
type S<T> = Set<T>;
type AType = {
mySet: Set<number>;
};
interface AnInterface {
mySet: Set<string>;
doSth(): Set<string>;
}
const setMock: S<string> = createMock<S<string>>();
const aType: AType = createMock<AType>();
const anInterface: AnInterface = createMock<AnInterface>();
const realSet: Set<string> = new Set();

expect(setMock.constructor).toBe(realSet.constructor);
expect(aType.mySet.constructor).toBe(realSet.constructor);
expect(anInterface.mySet.constructor).toBe(realSet.constructor);
expect(anInterface.doSth().constructor).toBe(realSet.constructor);
});

it('should set undefined for a not recognized type alias declaration', () => {
interface WithLiteralTypescriptType {
prop: InsertPosition;
Expand Down
1 change: 1 addition & 0 deletions ui/src/views/types-supported.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ class MyClass {
number: Number; // 0
promise: Promise<string>; // a promise that will resolve an empty string Promise.resolve("")
map: Map<string, string>; // new Map()
set: Set<string>; // new Set()
}
```
Expand Down

0 comments on commit 446c090

Please sign in to comment.