typescript-is
typescript-is copied to clipboard
Fail to check Enum in Record
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
.
typescript-is
does not support union key typed dynamic property.
What about using this library instead?
https://github.com/samchon/typescript-json#runtime-validators
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
.
Can you write an issue on my repo? I'll solve this problem in 3 hours.
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.
OK, I enrolled the issue and your feature would be supported soon. Wait an hour.
https://github.com/samchon/typescript-json/issues/237
@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
Note that,
is()
becomingtrue
is not bug and you must useequals()
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 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.
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