typescript-is icon indicating copy to clipboard operation
typescript-is copied to clipboard

Fail to check Enum in Record

Open thekingofcity opened this issue 2 years ago • 9 comments

data is not a valid Translation in the following code because kr should be ko, but is<Translation>(data) === true. Let me know if it is not supported yet.

import { is } from 'typescript-is';

enum LanguageCode {
    Arabic = 'ar',
    ChineseSimp = 'zh-Hans',
    ChineseTrad = 'zh-Hant',
    English = 'en',
    French = 'fr',
    German = 'de',
    Japanese = 'ja',
    Korean = 'ko', // <-- this line
    Portuguese = 'pt',
    Russian = 'ru',
}

type Translation = { [l in LanguageCode]?: string };

const data = {
    en: 'Line 1',
    kr: '1호선 ', // <-- this line
    'zh-Hans': '1号线',
    'zh-Hant': '1號線',
};

console.log(is<Translation>(data));

I ran it under ttypescript and ts-node by $Env:TS_NODE_COMPILER="ttypescript" and then node --loader ts-node/esm test.ts. typescript-is === 0.19.0 ttypescript === 1.5.13 typescript === 4.5.4.

thekingofcity avatar Feb 18 '22 08:02 thekingofcity

typescript-is does not support union key typed dynamic property.

What about using this library instead?

https://github.com/samchon/typescript-json#runtime-validators

samchon avatar Oct 10 '22 15:10 samchon

typescript-is does not support union key typed dynamic property.

What about using this library instead?

https://github.com/samchon/typescript-json#runtime-validators

Thanks for the suggestion. However, typescript-json will also print true in the test snippet I posted before.

import { is } from "typescript-json";

// ...
// same as before

tsconfig.json is configured with "transform": "typescript-json/lib/transform"

I ran with TS_NODE_COMPILER="ttypescript" node --loader ts-node/esm test.ts

typescript-json === 3.3.9 ttypescript === 1.5.13 typescript === 4.7.4.

thekingofcity avatar Oct 11 '22 06:10 thekingofcity

Can you write an issue on my repo? I'll solve this problem in 3 hours.

samchon avatar Oct 11 '22 09:10 samchon

Can you write an issue on my repo? I'll solve this problem in 3 hours.

Simply copying and pasting should help you create an issue unless you want someone helping you to increase your repo's activity.

thekingofcity avatar Oct 11 '22 10:10 thekingofcity

OK, I enrolled the issue and your feature would be supported soon. Wait an hour.

https://github.com/samchon/typescript-json/issues/237

samchon avatar Oct 11 '22 11:10 samchon

@thekingofcity Upgrade to 3.3.10, then your feature will work on.

Note that, is() becoming true is not bug and you must use equals() function instead.

For reference, typescript-is returns always true whether call is() or equals().

import { equals, is } from "TSON";

const enum LanguageCode {
    Arabic = "ar",
    ChineseSimp = "zh-Hans",
    ChineseTrad = "zh-Hant",
    English = "en",
    French = "fr",
    German = "de",
    Japanese = "ja",
    Korean = "ko", // <-- this line
    Portuguese = "pt",
    Russian = "ru",
}

type DynamicEnumeration = {
    [P in LanguageCode]?: string;
};

const data: DynamicEnumeration = {
    en: "Line 1",
    ko: "1호선", // <-- this line
    "zh-Hans": "1号线",
    "zh-Hant": "1號線",
};
(data as any).kr = "2호선";

console.log("is", is(data)); // true
console.log("equals", equals(data)); // false

samchon avatar Oct 11 '22 12:10 samchon

Note that, is() becoming true is not bug and you must use equals() function instead.

Thanks for pointing out this. I didn't notice there is a equals function and looks like it also works in 3.3.9. 😄

thekingofcity avatar Oct 12 '22 09:10 thekingofcity

@thekingofcity Nope, 3.3.9 doesn't identify the enumeration key type. The reason why equals() function of 3.3.9 returns true is that the old version considered the object does not have any property. Therefore, you'd better to update to the 3.3.10 version to get exact validation result.

samchon avatar Oct 12 '22 09:10 samchon

Maybe my device is still running 3.3.10 when I ran npm i [email protected] :(

Anyway, it works now and really thank you and your typescript-json project. I've stared your project and made a PR using this at railmapgen/rmg-palette#221

thekingofcity avatar Oct 12 '22 12:10 thekingofcity