Skip to content
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

fix: remove .value field from type declarations for boolean literal types #535

Merged
Merged
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
23 changes: 23 additions & 0 deletions src/types/typeGuards/literal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { describe, expect, it } from "vitest";

import { createSourceFileAndTypeChecker } from "../../test/utils";
import {
BooleanLiteralType,
isBigIntLiteralType,
isBooleanLiteralType,
isFalseLiteralType,
isLiteralType,
isNumberLiteralType,
Expand Down Expand Up @@ -40,3 +42,24 @@ describe.each([
});
});
});

describe("booleans don't have .value", () => {
for (const trueOrFalse of ["true", "false"]) {
it(`should show that ${trueOrFalse} is a boolean literal type but doesn't have a .value field`, () => {
const { sourceFile, typeChecker } = createSourceFileAndTypeChecker(`
declare const x: ${trueOrFalse};
`);

const node = (sourceFile.statements[0] as ts.VariableStatement)
.declarationList.declarations[0].name;

const type = typeChecker.getTypeAtLocation(node);

expect(isBooleanLiteralType(type)).toBe(true);
const booleanLiteralType = type as BooleanLiteralType;
expect(booleanLiteralType.intrinsicName).toEqual(trueOrFalse);

expect(booleanLiteralType).not.toHaveProperty("value");
});
}
});
10 changes: 5 additions & 5 deletions src/types/typeGuards/literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import { type FreshableIntrinsicType } from "./compound";
* i.e. Either a "true" or "false" literal.
* @category Type Types
*/
export interface BooleanLiteralType extends UnknownLiteralType {
export interface BooleanLiteralType extends FreshableIntrinsicType {
intrinsicName: "false" | "true";
value: boolean;
}

/**
Expand Down Expand Up @@ -55,7 +54,6 @@ export function isBigIntLiteralType(
*/
export interface FalseLiteralType extends BooleanLiteralType {
intrinsicName: "false";
value: false;
}

/**
Expand Down Expand Up @@ -150,7 +148,6 @@ export function isTemplateLiteralType(
*/
export interface TrueLiteralType extends BooleanLiteralType {
intrinsicName: "true";
value: true;
}

/**
Expand All @@ -171,14 +168,16 @@ export function isTrueLiteralType(type: ts.Type): type is TrueLiteralType {

/**
* `LiteralType` from typescript except that it allows for it to work on arbitrary types.
* @deprecated Use {@link FreshableIntrinsicType} instead.
* @category Type Types
*/
export interface UnknownLiteralType extends FreshableIntrinsicType {
value: unknown;
value?: unknown;
}

/**
* Test if a type is a {@link UnknownLiteralType}.
* @deprecated Use {@link isFreshableIntrinsicType} instead.
* @category Types - Type Guards
* @example
* ```ts
Expand All @@ -191,6 +190,7 @@ export interface UnknownLiteralType extends FreshableIntrinsicType {
*/
export function isUnknownLiteralType(
type: ts.Type,
// eslint-disable-next-line deprecation/deprecation
): type is UnknownLiteralType {
return isTypeFlagSet(type, ts.TypeFlags.Literal);
}
Loading