[Typescript] Create Type From Any Given Data in TypeScript

TypeScript can infer the type of a variable by looking at the data assigned to it. It also allows you to create a reusable type that it has inferred and later reuse it in your application. In this lesson, we look at the typeof operator and see how we can use it to create reusable types from data.

const user = {
  name: "John Doe",
  age: 27
};

type User = typeof user;

const user2: User = {
  age: 28,
  name: "Jane Doe"
};

console.log(user);
console.log(user2);
原文地址:https://www.cnblogs.com/Answer1215/p/14396500.html