函数作为其他函数的属性 - 正确定义

时间:2018-02-15 09:26:10

标签: typescript

如何为具有其他功能作为属性的函数定义接口?下面的示例不起作用,编译器抱怨 dat %>% mutate(new_var = gsub("data/monroe_20180214", "pooled", source_file), + new_var = gsub(" ", ".", new_var), + new_var = gsub("/", "_", new_var), + new_var = gsub("pooled_", "pooled/", new_var)) # A tibble: 6 x 2 source_file new_var <chr> <chr> 1 data/monroe_20180214/180131 WT PB d5/PB x10_01.tif pooled/180131.WT.PB.d5_PB.x10_01.tif 2 data/monroe_20180214/180131 WT PB d5/PB x10_02.tif pooled/180131.WT.PB.d5_PB.x10_02.tif 3 data/monroe_20180214/180131 WT PB d5/PB x10_03.tif pooled/180131.WT.PB.d5_PB.x10_03.tif 4 data/monroe_20180214/180131 WT PB d5/PB x10_04.tif pooled/180131.WT.PB.d5_PB.x10_04.tif 5 data/monroe_20180214/180131 WT PB d5/PB x10_05.tif pooled/180131.WT.PB.d5_PB.x10_05.tif 6 data/monroe_20180214/180131 WT PB d5/PB x10_06.tif pooled/180131.WT.PB.d5_PB.x10_06.tif

Property 'peek' does not exist on type '(name): string'

2 个答案:

答案 0 :(得分:2)

没有办法在Typescript中实现这样的功能,但你可以像现在一样构建它。问题是您必须明确键入Unable to resolve artifact: Unable to get dependency information: Unable to read the metadata file for artifact 'org.apache.tiles:tiles-core:jar': Cannot find parent: org.apache.tiles:tiles-parent for project: null:tiles-core:jar:null for project null:tiles-core:jar:null 08:52:47 org.apache.tiles:tiles-core:jar:2.0.6 08:52:47 08:52:47 from the specified remote repositories: 08:52:47 apache.snapshots (http://repository.apache.org/snapshots), 08:52:47 central (http://repo1.maven.org/maven2), 08:52:47 nexus (http://10.210.40.37:8081/nexus/content/groups/public) 08:52:47 08:52:47 Path to dependency: 08:52:47 1) org.apache.maven:super-pom:pom:2.0 08:52:47 2) com.p.v.a.legacy:bo:jar:3.113.0-SNAPSHOT 08:52:47 3) com.p.v.a.legacy:s2-common:jar:3.113.0-SNAPSHOT 08:52:47 4) org.apache.struts:struts2-tiles-plugin:jar:2.3.16.3 result。由于您分配的功能尚不具有MyReturnType属性,因此您可以执行以下两项操作之一:

  • 在界面
  • 上设置peek功能可选
  • 使用类型断言忽略错误。

我会使用第二种方法,因为您正在构建该函数,您将很快分配缺少的属性:

peek

答案 1 :(得分:2)

您还可以使用Object.assign创建混合类型:

interface MyReturnType {
  (): string;
  peek(): string; 
}

interface MyEvaluator {
  evaluator(): MyReturnType;
}

const myEvaluator: MyEvaluator = {
  evaluator() {
    return Object.assign(function () {
      return 'A';
    }, {
      peek() {
        return 'B'
      }
    });
  }
}