简体   繁体   English

如何在 Typescript 中扩展另一个时声明 A 或 B 类型的变量

[英]How to declare variable of type A or B when one extends the other in Typescript

I have a context provider which streams user data throughout the app.我有一个上下文提供程序,可以在整个应用程序中传输用户数据。

I have a Student interface:我有一个学生界面:

export interface Student extends User

I would like the provider to return user data of type Student or User as follows:我希望提供者返回StudentUser类型的用户数据,如下所示:

let userData: Student | User = null;

When trying to access a property only available to students, userData?.currentTeam , VS Code throws the following error:当尝试访问仅对学生可用的属性userData?.currentTeam时,VS Code 会引发以下错误:

Property 'currentTeam' does not exist on type 'Student | User'.
  Property 'currentTeam' does not exist on type 'User'.ts(2339)

I need help finding out why it is defaulting to the parent interface and how to allow the option of both.我需要帮助找出为什么它默认为父界面以及如何允许两者的选项。

This is standard behavior of unions: unless you do something to check what type you're dealing with, typescript will only allow you to access properties that exist on all members of the union.这是联合体的标准行为:除非您检查所处理的类型,否则 typescript 将只允许您访问联合体所有成员上存在的属性。

Here are some examples of how you can narrow down the type:以下是一些如何缩小类型范围的示例:

if ('currentTeam' in userData) {
  console.log(userData.currentTeam);
}

Or:或者:

if (userData instanceof Student) {
  console.log(UserData.currentTeam);
}

Or, change the types so the objects have a property saying what type they are, which lets you do a "discriminated union"或者,更改类型,使对象具有说明它们是什么类型的属性,这使您可以执行“有区别的联合”

interface User {
  type: 'user',
  // rest of the type here
}

interface Student extends User {
  type: 'student',
  currentTeam: // something,
  // rest of the type here
}

if (userData.type === 'student) {
  console.log(userData.currentTeam);
}

暂无
暂无

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

相关问题 当 Type B 以某种方式“扩展”Type A 时,如何停止 TypeScript 错误“Type A 没有与 Type B 相同的属性” - How to stop TypeScript error 'Type A has no properties in common with Type B' when Type B "extends" Type A in some way 如何将 state 变量声明为 Typescript 上的类型 - How to declare a state variable as a Type on Typescript 在React中播放Typescript系统时如何声明此现有变量的类型? - How to declare this existing variable's type when playing Typescript system in React? Typescript如何声明子类类型? - Typescript How to declare a subclass type? Object 或 Boolean 如何在 Typescript 中声明类型 - Object or Boolean how to declare type in Typescript 如何在 TypeScript 中将 function 类型声明为参数 - How to declare a function type as a parameter in TypeScript typescript中删除function的输入如何声明类型? - How to declare the type for the input of a delete function in typescript? 如何在 React 中使用 Typescript 在没有“类型中缺少索引签名”错误的情况下声明一个变量 - How to declare a variable before assign it in React using Typescript without “Index signature is missing in type” Error 如何根据同一类型中提到的键在打字稿中声明类型? - How to declare a type in typescript based on the keys mentioned in the same type? Typescript:如何声明一个包含所有扩展通用类型的类型的类型? - Typescript: how to declare a type that includes all types extending a common type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM