Skip to content

Fix #115: typeof usage on object types #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/transform-inline/visitor-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,14 @@ export function getPropertyInfo(parentType: ts.Type, symbol: ts.Symbol, visitorC
let isFunction: boolean | undefined = undefined;
let optional: boolean | undefined = undefined;

if ('valueDeclaration' in symbol) {
if ('type' in symbol) {
// Attempt to get it from 'type'

propertyType = (symbol as { type?: ts.Type }).type;
isMethod = false;
isFunction = false;
optional = ((symbol as ts.Symbol).flags & ts.SymbolFlags.Optional) !== 0;
} else if ('valueDeclaration' in symbol) {
// Attempt to get it from 'valueDeclaration'

const valueDeclaration = symbol.valueDeclaration;
Expand All @@ -94,13 +101,6 @@ export function getPropertyInfo(parentType: ts.Type, symbol: ts.Symbol, visitorC
propertyType = visitorContext.checker.getTypeFromTypeNode(valueDeclaration.type);
}
optional = !!valueDeclaration.questionToken;
} else if ('type' in symbol) {
// Attempt to get it from 'type'

propertyType = (symbol as { type?: ts.Type }).type;
isMethod = false;
isFunction = false;
optional = ((symbol as ts.Symbol).flags & ts.SymbolFlags.Optional) !== 0;
} else if ('getTypeOfPropertyOfType' in visitorContext.checker) {
// Attempt to get it from 'visitorContext.checker.getTypeOfPropertyOfType'

Expand Down
37 changes: 37 additions & 0 deletions test/issue-115.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as assert from 'assert';
import { is } from '../index';

/* https://github.com/woutervh-/typescript-is/issues/115 */

export const Status = {
enable: 'enable',
disable: 'disable'
} as const;

type StatusObject<Key extends keyof typeof Status> = {
status: (typeof Status)[Key];
};

const testObj = {
outer: 0
};

describe('is', () => {
describe('Accessing generic member of a type', () => {
it('should return true for the right member', () => {
assert.deepStrictEqual(is<StatusObject<'enable'>>({ status: Status.enable }), true);
});
it('should return false for the wrong member', () => {
assert.deepStrictEqual(is<StatusObject<'enable'>>({ status: Status.disable }), false);
});
});

describe('Using typeof instead of a type', () => {
it('should return true for the right member', () => {
assert.deepStrictEqual(is<typeof testObj>('test'), false);
});
it('should return false for the wrong member', () => {
assert.deepStrictEqual(is<typeof testObj>({ outer: 5 }), true);
});
});
});