简体   繁体   English

具有某些属性的接口函数(TypeScript)?

[英]Interface function with some properties (TypeScript)?

How can I use following interface?如何使用以下界面?

interface Something {
 (n: number): void,
 description: string
}

I try writing following.我试着写以下。

var sth: Something = {
    description: "Description"
}

sth = (n: number) => {
    console.log("Something");
}

or或者

var sth: Something = {
    (n: number) => {
        console.log("Something");
    },
    description: "Description"
}

But they both give errors.但他们都给出了错误。

You cannot directly create an object which is both a function and has other properties.您不能直接创建既是函数又具有其他属性的对象。 You have to do it in two steps, first create a function, then assign it another prop, then assign the result to a variable of your interface:您必须分两步完成,首先创建一个函数,然后为其分配另一个 prop,然后将结果分配给您界面的一个变量:

interface Something {
  (n: number): void;
  description: string;
}

function fn(n: number) {}

fn.description = 'foo';

const h: Something = fn;

You can do it in one of few ways.您可以通过以下几种方式之一来完成。 Each fits slightly odd with functions with properties but all are viable, depending on the use case:每个都适合具有属性的函数,但都是可行的,具体取决于用例:

Create the function then add the property创建函数然后添加属性

There is no syntax for creating a function with a property.没有用于创建具有属性的函数的语法。 You have to do both separately:你必须分开做:

var sth: Something = ((n: number) => {
    console.log(n + 2);
}) as Something;
sth.description = "Description";

Playground Link 游乐场链接

The as Something assertion is needed here to tell the compiler to treat the function as Something , otherwise you cannot add the description .这里需要as Something断言来告诉编译器将函数视为Something ,否则您无法添加description

Object.assign()

This allows declaring everything at once:这允许一次声明所有内容:

var sth: Something = Object.assign(
  (n: number) => {
    console.log(n + 2);
  },
  { description: "Description"}
);

Playground Link 游乐场链接

Object.assign is typed to return any so the type declaration sth: Something makes sure it is treated as the correct object afterwards. Object.assign被输入为返回any所以类型声明sth: Something确保之后它被视为正确的对象。

Object.defineProperty()

Another way to create an object and add a property.另一种创建对象添加属性的方法。 It allows more control over the property, like making it non-writable, if needed.如果需要,它允许对属性进行更多控制,例如使其不可写。

var sth: Something = Object.defineProperty(
  (n: number) => {
    console.log(n + 2);
  },
  "description",
  { value: "Description"}
) as Something;

Playground Link 游乐场链接

The as Something assertion is needed because Object.defineProperty() is typed to return the same as the first argument.因为Object.defineProperty()的类型与第一个参数相同,所以需要as Something断言。 So it returns (n: number) => void .所以它返回(n: number) => void

Object.defineProperties()

Very similar to Object.defineProperty() but allows multiple properties to be configured at once.Object.defineProperty()非常相似,但允许一次配置多个属性。

var sth: Something = Object.defineProperties(
  (n: number) => {
    console.log(n + 2);
  },
  { 
    description: { value: "Description" }
  }
) as Something;

Playground Link 游乐场链接

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM