Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 类型脚本类型封装_Javascript_Typescript_Typescript Typings_Encapsulation - Fatal编程技术网

Javascript 类型脚本类型封装

Javascript 类型脚本类型封装,javascript,typescript,typescript-typings,encapsulation,Javascript,Typescript,Typescript Typings,Encapsulation,在下面的示例中,我试图描述一种复杂的typescript类型,稍后将使用它FinalType。问题是,由于其复杂性,此类型需要声明污染页面的中介类型IntermediaryType/IntermediaryType2 我的问题是,有没有办法使中介类型不可访问,并且只允许使用FinalType 我已经看到,您可以使用括号括起部分代码,如: { type IntermediaryType = { decisiveKey: true, mutatedKey: (params:

在下面的示例中,我试图描述一种复杂的typescript类型,稍后将使用它
FinalType
。问题是,由于其复杂性,此类型需要声明污染页面的中介类型
IntermediaryType
/
IntermediaryType2


我的问题是,有没有办法使中介类型不可访问,并且只允许使用
FinalType


我已经看到,您可以使用括号括起部分代码,如:

{
  type IntermediaryType = {
    decisiveKey: true,
    mutatedKey: (params: any) => void,
  } | {
    decisiveKey: false,
    mutatedKey?: false,
  }

  interface IntermediaryType2 {
    foo?: string,
    bar?: boolean,
  }

  type FinalType = IntermediaryType & IntermediaryType2;
}

const Foo = (param: FinalType) => {}

Foo({
  decisiveKey: true,
  mutatedKey: () => {},
});
但是我显然无法访问
FinalType
。我尝试过使用
return
export
,但都不起作用

理想的情况是:

type FinalType = {
  type IntermediaryType = {
    decisiveKey: true,
    mutatedKey: (params: any) => void,
  } | {
    decisiveKey: false,
    mutatedKey?: false,
  }

  interface IntermediaryType2 {
    foo?: string,
    bar?: boolean,
  }

  return IntermediaryType & IntermediaryType2;
}

const Foo = (param: FinalType) => {}

Foo({
  decisiveKey: true,
  mutatedKey: () => {},
});

有线索吗



使用@Aluan Haddad answer,目前我能达到的最佳效果是:

namespace _ {
    type IntermediaryType = {
        decisiveKey: true,
        mutatedKey: (params: any) => void,
    } | {
        decisiveKey: false,
        mutatedKey?: false,
    }

    interface IntermediaryType2 {
        foo?: string,
        bar?: boolean,
    }

    export type FinalType = IntermediaryType & IntermediaryType2;
}; type FinalType = _.FinalType;

const Foo = (param: FinalType) => {}

Foo({
  decisiveKey: true,
  mutatedKey: () => {},
});

我想在上面加一些语法糖

您真正想要做的,为类型引入任意作用域,可以通过TypeScript命名空间来完成

declare namespace myNamespace {
  type IntermediaryType = {
    decisiveKey: true,
    mutatedKey: (params: any) => void,
  } | {
    decisiveKey: false,
    mutatedKey?: false,
  }

  interface IntermediaryType2 {
    foo?: string,
    bar?: boolean,
  }

  export type FinalType = IntermediaryType & IntermediaryType2;
}

const Foo = (param: myNamespace.FinalType) => {}

Foo({
  decisiveKey: true,
  mutatedKey: () => {},
});
但是,如果您编写的是现代代码,因此使用的是模块,那么几乎总是应该避免使用名称空间。幸运的是,如果您使用的是模块,那么解决方案就更简单了;不要导出中间类型

type IntermediaryType = {
  decisiveKey: true,
  mutatedKey: (params: any) => void,
} | {
  decisiveKey: false,
  mutatedKey?: false,
}

interface IntermediaryType2 {
  foo?: string,
  bar?: boolean,
}

export type FinalType = IntermediaryType & IntermediaryType2;

export const Foo = (param: FinalType) => {}

Foo({
  decisiveKey: true,
  mutatedKey: () => {},
});

我不确定你的前言

由于其复杂性,此类型需要声明中间类型

这就编译了

type FinalType = (
  | {
      decisiveKey: true;
      mutatedKey: (params: any) => void;
    }
  | {
      decisiveKey: false;
      mutatedKey?: false;
    }
) & {
  foo?: string;
  bar?: boolean;
};

const Foo = (param: FinalType) => {};

Foo({
  decisiveKey: true,
  mutatedKey: () => {}
});

希望这能有所帮助。

名称空间可以做到这一点,但使用起来仍然不方便。另外重新声明类型将使其
type FinalType=namespace.FinalType。关于第二个建议,在本地文件范围内,导出不能解决任何问题。感谢您的回答如果只是在一个文件中定义范围,那么名称空间是合适的,即使是在一个模块中。这是一个权衡。谢谢你的回答。因此,您的解决方案是抑制
类型
接口
关键字。在某些情况下可能是一个解决方案。但是,如果应用于大型和深层类型,则似乎有点复杂,以公开构建所需的所有内容,所有公开的内容(在您的示例中是一种类型)都是类型脚本性质的。如果您不想公开任何中间类型,您只需要使用本机类型。我认为您应该通过不同的文件组织您的体系结构,在这种情况下,只导出FinalType。通过这种方式,您将拥有一个包含所有中间类型和最终类型的文件,但是,只有最终类型可以在外部文件中使用。这是一个架构建议,在90%的情况下都很好。但是当您键入使用稍微修改过的现有类型的privates方法时;我几乎看不出放入外部文件的价值。
type FinalType = (
  | {
      decisiveKey: true;
      mutatedKey: (params: any) => void;
    }
  | {
      decisiveKey: false;
      mutatedKey?: false;
    }
) & {
  foo?: string;
  bar?: boolean;
};

const Foo = (param: FinalType) => {};

Foo({
  decisiveKey: true,
  mutatedKey: () => {}
});