相关疑难解决方法(0)

从类型中排除属性

我想从类型中排除一个属性.我怎样才能做到这一点?

比如我有

interface XYZ {
  x: number;
  y: number;
  z: number;
}
Run Code Online (Sandbox Code Playgroud)

我想排除属性z得到

type XY = { x: number, y: number };
Run Code Online (Sandbox Code Playgroud)

typescript

111
推荐指数
7
解决办法
5万
查看次数

如何覆盖 Typescript 中交集类型的属性?

假设我有以下类型:

type BaseAnimal = {
  species: string
  owner: boolean
}

type Cat = BaseAnimal & {
  species: 'cat'
  hasTail: boolean
}

type Dog = BaseAnimal & {
  species: 'dog'
  likesWalks: boolean
}

type Animal = Cat | Dog
Run Code Online (Sandbox Code Playgroud)

我想创建一个名为 的类型,除了AnimalParams属性是一个字符串之外,它与其他类型相同Animal owner

以下任何一项我都做不到。

// This seems to keep the owner property from Animal instead of overwriting
// So it raises an error if I try to specify owner as a string
type AnimalParams = Animal & …
Run Code Online (Sandbox Code Playgroud)

types typescript .d.ts

18
推荐指数
3
解决办法
1万
查看次数

在打字稿中找不到名称“省略”

背景

我正在上创建我的自定义index.d.ts文件src/@types/index.d.ts。我需要像下面这样合并我的类型。

// src/@types/index.d.ts
declare namespace Admin {
  interface InitialStateFromDB {
    teamSettings: {
      teamPasswords: TeamPassword[],
      teamCount: number
    },
    adminPasswords: string,
    postInfos: PostInfo[] | undefined
  }
  interface InitialState extends Omit<InitialStateFromDB, 'adminPasswords'> {
    adminPasswords: AdminPassword
  }
}
Run Code Online (Sandbox Code Playgroud)

问题

我得到了错误:Cannot find name 'Omit'.ts(2304)VSCode Intellisense给出了。但是编译效果很好。

所以,我做了省略类型type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>(在这里复制)

编译后,我又遇到了一个错误: 'Omit' was also declared here.

在这种情况下我该怎么办?只是忽略IntelliSense的错误?

typescript

8
推荐指数
2
解决办法
2730
查看次数

Typescript:将窗口上的属性更改为可选?

默认的 TS 类型window.NotificationNotification,但由于 Web 通知 API 只有 90% 的浏览器支持,因此应该是Notification | undefined。我最近遇到一个错误,我在使用时window.Notification没有检查它是否已定义。

有没有办法覆盖 的属性window,例如使它们可选?我尝试了这个,但没有成功:

declare global {
  interface Window {
    Notification?: Notification;
  }
}
Run Code Online (Sandbox Code Playgroud)

javascript typescript

6
推荐指数
1
解决办法
145
查看次数

如何覆盖打字稿中的属性?

由于当前的语言限制,这可能无法实现,但我使用的是最新的 TS (1.8.10) 并且遇到了 ui-grid 类型的问题。该isRowSelectable物业上IGridOptions被定义为一个可选的布尔但文件说,这是一个功能(它是)。我试图将布尔属性覆盖为返回布尔值的函数。

通常,我只是扩展打字界面并执行我需要的操作,但这在这种情况下不起作用。这是我所拥有的:

interface ISelectionGridOptions extends IGridOptions {
  isRowSelectable: (row: IGridRowOf<Profile>) => boolean;
}
Run Code Online (Sandbox Code Playgroud)

中的相关字段IGridOptions是:

export interface IGridOptions {
  ...
  isRowSelectable?: boolean
  ...
}
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

(41,11): error TS2430: Interface 'ISelectionGridOptions' incorrectly extends interface 'IGridOptionsOf<any>'.
  Types of property 'isRowSelectable' are incompatible.
    Type '(row: IGridRowOf<Profile>) => boolean' is not assignable to type 'boolean'.
Run Code Online (Sandbox Code Playgroud)

缺少修复核心类型定义,有没有办法在我的代码中解决这个问题?

typescript

5
推荐指数
3
解决办法
2万
查看次数

Typescript:通过模块增强在内部对象上添加属性

考虑外部 (npm) 模块extmod在其声明文件中公开以下接口:

interface Options {
  somevar?: string;
  suboptions?: {
    somesubvar?: string;
  };
}
Run Code Online (Sandbox Code Playgroud)

如何通过模块增强somesubvar2在内部添加属性?suboptions

我在文件中尝试了以下操作extmod.d.ts

declare module 'extmod' {
  interface Options {
    suboptions?: {
      somesubvar2?: string;
    };
  }
}
Run Code Online (Sandbox Code Playgroud)

但它会引发以下错误:

error TS2687: All declarations of 'suboptions' must have identical modifiers.
Run Code Online (Sandbox Code Playgroud)
error TS2717: Subsequent property declarations must have the same type.  Property 'suboptions' must be of type '<SNIP>', but here has type '{ somesubvar2: string; }'.
Run Code Online (Sandbox Code Playgroud)

typescript module-augmentation

5
推荐指数
1
解决办法
960
查看次数

如何在TypeScript中覆盖类型属性

例如,我有

type Line = {
  start: Point;
  end: Point;
  color: string; //'cyan'/'aquablue'/...
}
Run Code Online (Sandbox Code Playgroud)

但是现在我想在Line的基础上创建新的线型,以便将颜色存储为数字:

type HexColorLine = Point & {
  color: number;
}
Run Code Online (Sandbox Code Playgroud)

现在我希望HexColorPoint类型等于

{
  start: Point;
  end: Point;
  color: number;
}
Run Code Online (Sandbox Code Playgroud)

但这等于

{
  start: Point;
  end: Point;
  color: string | number;
}
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以使用一些简短的语法覆盖但不扩展prop类型?我真的需要为此定义全新的类型吗?

typescript

4
推荐指数
5
解决办法
2675
查看次数

在TypeScript中覆盖接口的属性

我知道禁止在扩展接口中覆盖接口的属性,修改它们的类型.

我正在寻找一种替代解决方案,它可以让我不复制第一个界面的内容(它非常大).

这是我的第一个天真的方法.给定基接口:

interface OrginalInterface {
    title?: string;
    text?: string;
    anotherProperty?: SomeType;
    // lots of other properties
}
Run Code Online (Sandbox Code Playgroud)

此接口在库中定义.我无法修改它(例如,添加泛型)只是为了满足我在扩展接口中的需求.

在包装库(我的)使用的扩展接口中,我想重用现有的接口,同时使一些字段具有不同的类型:

interface ExtendedInterface extends OriginalInterface {
    title?: string | ReactElement<any>;
    text?: string | ReactElement<any>;
}
Run Code Online (Sandbox Code Playgroud)

但这是不可能的.

error TS2430: Interface 'ExtendedInterface' incorrectly extends interface 'OriginalInterface'.
  Types of property 'title' are incompatible.
    Type 'ReactElement<any>' is not assignable to type 'string'.
Run Code Online (Sandbox Code Playgroud)

我还尝试将两个接口合并在一起:

type Extended = OriginalInterface & NewInterfaceWithOverridingPropertiesOnly;
Run Code Online (Sandbox Code Playgroud)

虽然这通过了编译,但它不起作用.如果使用此类型声明变量,则只能分配具有兼容结构的对象OriginalInterface.

我觉得打字稿的类型系统没有为我提供任何其他的方式来表达我需要声明的新型衍生OrginalInterface.我不需要新类型可分配给OriginalInterface; 我只是需要它来重用大多数属性OriginalInterface.

我需要类似映射类型的东西,其中包含属性受影响的条件.也许来自预发行TypeScript 2.8的 …

types overriding typescript

3
推荐指数
2
解决办法
3852
查看次数

打字稿:如何仅从类型中提取可选键?

假设我有一个这样的类型:

type User = {
  uid: string,
  displayName?: string,
  bestFriend?: string,
}
Run Code Online (Sandbox Code Playgroud)

是否可以使用某种映射类型从我的用户类型中提取可选属性?我正在寻找如何定义以下 OptioanlProperties<T>类型。

type OptionalUserProperties = OptionalProperties<User>
// type OptionalUserProperties = "displayName" | "bestFriend"
Run Code Online (Sandbox Code Playgroud)

我的用例是计算UpdateOf<User>允许特定“操作”值的类型,例如DeleteProperty将其分配给基本类型中可选的键。

export type UpdateOf<T> =
  // optional fields may be their own type, or the special DeleteProperty type.
  { [P in OptionalProperties<T>]?: T[P] | DeleteProperty } &
  // required fields may be changed, but cannot be deleted.
  { [P in Diff<keyof T, OptionalProperties<T>>]?: T[P] }
Run Code Online (Sandbox Code Playgroud)

typescript

3
推荐指数
1
解决办法
882
查看次数