Skip to content

Commit

Permalink
Improve typing of symToStr
Browse files Browse the repository at this point in the history
  • Loading branch information
garronej committed Sep 16, 2023
1 parent 4aea4f9 commit 0f1a9fa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/symToStr.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/** @see <https://docs.tsafe.dev/main/symtostr> */
export function symToStr<T extends Record<string, unknown>>(wrap: T): keyof T {
export function symToStr<T extends Record<string, unknown>>(wrap: T): Exclude<keyof T, symbol | number> {
// @ts-expect-error: We know better
return Object.keys(wrap)[0];
}
32 changes: 32 additions & 0 deletions test/symToStr.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { symToStr } from "../src/symToStr";
import { Reflect } from "../src/Reflect";
import { assert } from "../src/assert";
import type { Equals } from "../src/Equals";

{
const theName = null;

const expected = "theName" as const;

const got = symToStr({ theName });

assert<Equals<typeof expected, typeof got>>();
}

{
const expected = "theName" as const;

const got = symToStr({ "theName": undefined });

assert<Equals<typeof expected, typeof got>>();
}

{
type Expected = string;

const got = symToStr(Reflect<any>());

assert<Equals<Expected, typeof got>>();
}

0 comments on commit 0f1a9fa

Please sign in to comment.