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

Fail to check Enum in Record #123

Open
thekingofcity opened this issue Feb 18, 2022 · 9 comments
Open

Fail to check Enum in Record #123

thekingofcity opened this issue Feb 18, 2022 · 9 comments

Comments

@thekingofcity
Copy link

thekingofcity commented Feb 18, 2022

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.

@samchon
Copy link
Contributor

samchon commented Oct 10, 2022

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

What about using this library instead?

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

@thekingofcity
Copy link
Author

thekingofcity commented Oct 11, 2022

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.

@samchon
Copy link
Contributor

samchon commented Oct 11, 2022

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

@thekingofcity
Copy link
Author

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.

@samchon
Copy link
Contributor

samchon commented Oct 11, 2022

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

samchon/typia#237

@samchon
Copy link
Contributor

samchon commented Oct 11, 2022

@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

@thekingofcity
Copy link
Author

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. 😄

@samchon
Copy link
Contributor

samchon commented Oct 12, 2022

@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.

@thekingofcity
Copy link
Author

thekingofcity commented Oct 12, 2022

Maybe my device is still running 3.3.10 when I ran npm i typescript-json@3.3.9 :(

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants