简体   繁体   English

导入管道引发的Typescript属性不存在错误

[英]Importing pipe throwing Typescript property does not exist error

I'm using ionic and have created a custom pipe that takes an object representing image data and returns a URL to that image. 我正在使用ionic,并创建了一个自定义管道,该管道接受一个表示图像数据的对象,并返回该图像的URL。

The pipe file looks like this... 管道文件看起来像这样...

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'eventImageLink',
})
export class EventImageLinkPipe implements PipeTransform {
  /**
   * Takes a value and makes it lowercase.
   */
  transform(value: string, ...args) {
    if(value){
      return `//res.cloudinary.com/***/image/upload/w_${window.screen.width},d_item_placeholder_e0lign.jpg/${value.image_version}/${args[0]}/${value.original_image_name}`;
    }
  }
}

I have created a pipes.module.ts file in the pipes folder that imports my pipes... 我已经在导入我的管道的pipes文件夹中创建了pipes.module.ts文件...

import { NgModule } from '@angular/core';
import { EventImageLinkPipe } from './event-image-link/event-image-link';

@NgModule({
  declarations: [
    EventImageLinkPipe
  ],
  imports: [

  ],
  exports: [
    EventImageLinkPipe
  ]
})
export class PipesModule {}

Now before I even import the PipesModule into a component for use I am getting the following error... 现在,在将PipesModule导入组件以供使用之前,我得到以下错误...

Typescript Error 打字稿错误

Property 'image_version' does not exist on type 'string'. 类型'string'上不存在属性'image_version'。

I can't understand why this error is coming up when I haven't even attempted to use it yet and it's quite annoying really. 我什至还没有尝试使用它时,却不明白为什么会出现此错误,这确实很烦人。 Can someone explain why the pipe is being executed and how I can prevent this error from throwing when I haven't passed any data into the pipe yet? 谁能解释为什么执行管道以及在尚未将任何数据传递到管道中时如何防止抛出此错误?

Well, you declared input value: string which means that you will pass object of type string in it's place in the future. 好吧,您声明了输入value: string ,这意味着将来将在其位置传递string类型的对象。 Type of string does not have property of an image_version as the error says, that is an error and your code simply won't work. 如错误所述, string类型不具有image_version属性,这是一个错误,您的代码根本无法工作。 You're trying to call value.image_version as part of your return result, you will need to change value to be of a proper type, not sure which it is in your case. 您尝试调用value.image_version作为返回结果的一部分,您将需要将value更改为正确的类型,不确定是哪种情况。

You could probably have a custom class for it: 您可能为此有一个自定义类:

export class MyImage {
    image_version: string;
    original_image_name: string;
}

You can then declare a value: MyImage as type of MyImage class and Typescript will be fine with it since it contains both properties, image_version and original_image_name unlike string type. 然后,您可以声明一个value: MyImage作为MyImage类和Typescript的类型会很好用,因为它包含与string类型不同的属性image_versionoriginal_image_name

Also, nothing is being executed really, it's just the TypeScript's static-type checking, which prevents you from doing stupid mistakes like this. 另外,实际上并没有执行任何操作,只是TypeScript的静态类型检查,它可以防止您犯此类愚蠢的错误。 If you don't want to use static-type checking features, you could also just set it as value with no type specified. 如果您不想使用静态类型检查功能,也可以将其设置为没有指定类型的value That way you can pass whatever you want and it will crash at runtime if it's a wrong type of an object instead. 这样,您可以传递所需的任何内容,并且如果对象类型错误,它将在运行时崩溃。

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

相关问题 Map 和 FlatMap 抛出错误“属性 &#39;map&#39; 在 Typescript 中的类型 &#39;unknown&#39; 上不存在 - Map and FlatMap throwing error "Property 'map' does not exist on type 'unknown' in Typescript 打字稿类型错误属性不存在 - Typescript type error property does not exist TypeScript static 方法中的属性不存在错误 - Property does not exist error in TypeScript static method 打字稿:属性不存在 - Typescript: Property does not exist 类型{}不存在Typescript属性 - Typescript property does not exist on type {} 类型 [] 打字稿上不存在属性 - property does not exist on type [] typescript 属性在类型上不存在-TypeScript - Property does not exist on type - TypeScript TypeScript构建错误:“ IStateParamsService”类型不存在属性 - TypeScript Build Error : Property does not exist on type 'IStateParamsService' 打字稿错误:类型“元素”上不存在属性“包含”。 - Typescript error :Property 'contains' does not exist on type 'Element'.? "<i>TypeScript error: Property &#39;children&#39; does not exist on type &#39;{ children?: ReactNode;<\/i> TypeScript 错误:类型 &#39;{ children?: ReactNode; 上不存在属性 &#39;children&#39;<\/b> <i>}&#39;<\/i> }&#39;<\/b>" - TypeScript error: Property 'children' does not exist on type '{ children?: ReactNode; }'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM