简体   繁体   English

从 JSON 文件推断字符串文字类型

[英]Infer string literal type from a JSON file

I am reading from a large JSON file.我正在读取一个大型 JSON 文件。
TypeScript is smart enough to infer the types of all the properties except one . TypeScript 足够聪明,可以推断除一个之外的所有属性的类型

A simplified example:一个简化的例子:

type Animal = 'bear' | 'cat' | 'dog';

const data = {
  name: 'Max',
  age: 3,
  animal: 'dog',
  // 100s other properties from JSON file...
};

let theName: string = data.name; // perfect
let theAge: number = data.age; // perfect
let theAnimal: Animal = data.animal; // Error: Type 'string' is not assignable to type 'Animal'

( link to playground ) 链接到操场

data.animal is used in several places, so I'm trying to avoid using as Animal everywhere. data.animal在几个地方使用,所以我试图避免as Animal任何地方使用as Animal

What is the best way to fix this issue?解决此问题的最佳方法是什么?
Is there any simple way I can tell the code that data.animal is an Animal ?有什么简单的方法可以告诉代码data.animalAnimal吗?

You can use sum types and merge 2 definitions - the original definition of your data and the animal:Animal definitions.您可以使用总和类型并合并 2 个定义 - 数据的原始定义和动物:动物定义。

type Animal = 'bear' | 'cat' | 'dog';

// the keys your want to exert
type DataWithAnimal = { [P in 'animal']: Animal } ;

const data = {
  name: 'Max',
  age: 3,
  animal: 'dog',
  // 100s other properties from JSON file...
};

// original data type
type DataType = typeof data;

// merge the 2 type definitions
type Data = DataType & DataWithAnimal;

// cast old type to new type
const typeData: Data = data as Data;

let theName: string = typeData.name; // perfect
let theAge: number = typeData.age; // perfect
let theAnimal: Animal = typeData.animal; // also perfect

What about do like this?这样做怎么样?

type Animal = 'bear' | 'cat' | 'dog';

type Data = {
  name: string;
  age: number;
  animal: Animal;
}
const data: Data = {
  name: 'Max',
  age: 3,
  animal: 'dog',
  // 100s other properties from JSON file...
};

let theName: string = data.name; // perfect
let theAge: number = data.age; // perfect
let theAnimal: Animal = data.animal;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从字符串操作推断字符串文字类型 - Infer string literal type from string manipulation 从返回类型推断出窄字符串文字类型 - Infer to a narrow string literal type from return type 是否可以从字符串模板文字中推断泛型类型 - Is it possible to infer generic type from inside string template literal Typescript从三元条件推断字符串文字 - typescript infer string literal from ternary conditional 在TypeScript中,是否可以从字符串的输入类型推断出区分联合的字符串文字类型? - In TypeScript is it possible to infer string literal types for Discriminated Unions from input type of string? 打字稿:从应用于键对象的字符串文字联合推断正确的类型 - Typescript: Infer correct type from string literal union applied on keyed object typescript 可以在没有`as const`的情况下推断字符串文字类型 - can typescript infer the string literal type without `as const` TypeScript:根据字符串文字属性一般推断联合类型成员 - TypeScript: generically infer union type member based on a string literal property 如何从 TS 文字联合类型推断 object 的键和值? - How to infer keys and values of an object from TS literal union type? 从字符串参数推断返回类型 - Infer return type from string parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM