[Telemetry-Tools] Add support for Omit<> (#121730)

This commit is contained in:
Alejandro Fernández Haro 2021-12-21 11:48:48 +01:00 committed by GitHub
parent e28340071b
commit cc3772c9e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 1 deletions

View file

@ -156,4 +156,14 @@ describe('getDescriptor', () => {
prop2: { kind: ts.SyntaxKind.StringKeyword, type: 'StringKeyword' },
});
});
it('serializes OmitIndexedAccessType', () => {
const usageInterface = usageInterfaces.get('OmitIndexedAccessType')!;
const descriptor = getDescriptor(usageInterface, tsProgram);
expect(descriptor).toEqual({
prop3: { kind: ts.SyntaxKind.StringKeyword, type: 'StringKeyword' },
prop4: { kind: ts.SyntaxKind.StringLiteral, type: 'StringLiteral' },
prop5: { kind: ts.SyntaxKind.FirstLiteralToken, type: 'FirstLiteralToken' },
});
});
});

View file

@ -7,7 +7,7 @@
*/
import * as ts from 'typescript';
import { uniqBy, pick } from 'lodash';
import { uniqBy, pick, omit } from 'lodash';
import {
getResolvedModuleSourceFile,
getIdentifierDeclarationFromSource,
@ -236,6 +236,12 @@ export function getDescriptor(node: ts.Node, program: ts.Program): Descriptor |
const pickPropNames = getConstraints(node.typeArguments![1], program);
return pick(parentDescriptor, pickPropNames);
}
// Support `Omit<SOMETHING, 'prop1' | 'prop2'>`
if (symbolName === 'Omit') {
const parentDescriptor = getDescriptor(node.typeArguments![0], program);
const omitPropNames = getConstraints(node.typeArguments![1], program);
return omit(parentDescriptor, omitPropNames);
}
const declaration = (symbol?.getDeclarations() || [])[0];
if (declaration) {

View file

@ -65,3 +65,4 @@ export type RecordWithKnownProps = Record<MappedTypeProps, number>;
export type RecordWithKnownAllProps = Record<MappedTypeAllProps, number>;
export type IndexedAccessType = Pick<WithUnion, 'prop1' | 'prop2'>;
export type OmitIndexedAccessType = Omit<WithUnion, 'prop1' | 'prop2'>;