Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.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
是否可以使用';这';像typescript中那样的关键字?_Typescript - Fatal编程技术网

是否可以使用';这';像typescript中那样的关键字?

是否可以使用';这';像typescript中那样的关键字?,typescript,Typescript,例如: // ===== Declaration ===== // class A { CONSTS_TYPE: { [key: string]: [any] } CONSTS: { [key in keyof this['CONSTS_TYPE']]: key } foo<T extends keyof this['CONSTS_TYPE'] | string>( type: T, callback: (...args: T extends keyof

例如:

// ===== Declaration ===== //
class A {
  CONSTS_TYPE: { [key: string]: [any] }
  CONSTS: { [key in keyof this['CONSTS_TYPE']]: key }
  foo<T extends keyof this['CONSTS_TYPE'] | string>(
    type: T,
    callback: (...args: T extends keyof this['CONSTS_TYPE'] ? this['CONSTS_TYPE'][T] : any) => any
  ) { /** some implementation*/ }
}
class B extends A {
  CONSTS_TYPE: {
    aa: [number],
    bb: [string]
  }
  // Here is the problem
  // Type '{ aa: "aa"; bb: "bb"; }' is not assignable to type '{ [key in keyof this["CONSTS_TYPE"]]: key; }'.(2322)
  CONSTS: { [key in keyof this['CONSTS_TYPE']]: key } = {
    aa: 'aa',
    bb: 'bb'
  }
}

// ===== Call ===== //
const b = new B;
b.foo(b.CONSTS.aa, (arg) => {
  // so that i can know 'arg' is a 'number' type
  arg // number type
});
/==声明=//
甲级{
常量类型:{[key:string]:[any]}
常量:{[key in keyof this['CONSTS_TYPE']]:key}
福(
类型:T,
回调:(…args:T扩展此['CONSTS_TYPE']?此['CONSTS_TYPE'][T]:any)=>any
){/**一些实现*/}
}
B类扩展了A类{
常数类型:{
aa:[编号],
bb:[字符串]
}
//问题就在这里
//类型“{aa:aa;bb:bb”;}”不能分配给类型“{[key in keyof this[“CONSTS_Type”]]:key;}”。(2322)
常量:{[key in keyof this['CONSTS_TYPE']]:key}={
aa:‘aa’,
bb:‘bb’
}
}
//====呼叫========//
常数b=新的b;
b、 foo(b.CONSTS.aa,(arg)=>{
//这样我就可以知道“arg”是“number”类型
arg//数字类型
});
它工作得很好,但不太好

我知道“/@ts ignore”将非常有效

但我认为可能还有其他解决办法


因此,我认为您的代码存在一些问题:

  • 您应该尽可能避免使用
    @ts ignore
  • aa:'aa',
    不是一个数字,应该给您带来一个错误。这不是你实施它的方式的原因
  • …args:T
    中,
    T
    是一个数组,而不是您认为的一个参数
  • 为什么在
    foo
    中使用
    …args

以下是我认为对您有帮助的内容:

// ===== Declaration ===== //
type ValueOf<T> = T[keyof T];

abstract class A {
  abstract CONSTS: { [key: string]: any }

  foo<T extends ValueOf<this['CONSTS']>>(
    type: T,
    callback: (arg: T) => any
  ) { /** some implementation*/ }
}

class B extends A {
  CONSTS: {
    aa: number,
    bb: string
  } = {
    aa: 5,
    bb: 'bb'
  }
}

// ===== Call ===== //
const b = new B;
b.foo(b.CONSTS.bb, (arg) => {
  // so that i can know 'arg' is a 'string' type
  arg // string type
});
/==声明=//
类型值of=T[keyof T];
抽象A类{
抽象常量:{[key:string]:any}
福(
类型:T,
回调:(arg:T)=>任何
){/**一些实现*/}
}
B类扩展了A类{
常数:{
aa:号码,
bb:字符串
} = {
aa:5,
bb:‘bb’
}
}
//====呼叫========//
常数b=新的b;
b、 foo(b.CONSTS.bb,(arg)=>{
//这样我就可以知道“arg”是“string”类型
arg//字符串类型
});

您实际上寻找的不是泛型类吗

declare class Foo<T extends Record<string, [any]>> {
  CONST_TYPES: T;
  CONSTS: {
    [K in keyof T]: K
  }

  constructor(types: T);

  foo<U extends keyof T>(type: U, callback: (value: T[U]) => any): any;
}

const foo = new Foo({ aa: [42], bb: ['foo'] });

foo.foo(foo.CONSTS.bb, (arg) => {
  arg // [string]
})
声明类Foo{
常数类型:T;
常数:{
[K in keyof T]:K
}
建造师(类型:T);
foo(type:U,callback:(value:T[U])=>any):any;
}
constfoo=newfoo({aa:[42],bb:['foo']});
foo.foo(foo.CONSTS.bb,(arg)=>{
arg//[string]
})

我终于找到了解决办法

// ===== Declaration ===== //
class A<T extends Record<string, [any?]>> {

    foo<U extends keyof T | string>(
        type: U,
        callback: (...args: U extends keyof T ? T[U] : any) => any
    ) { /** some implementation*/ }
}

type T_BConstsType = {
    aa: [number],
    bb: [string]
}

class B extends A<T_BConstsType> {

    CONSTS: { [key in keyof T_BConstsType]: key } = {
        aa: 'aa',
        bb: 'bb'
    }
}

// ===== Call ===== //
const b = new B;
b.foo(b.CONSTS.aa, (arg) => {
    // so that i can know 'arg' is a 'number' type
    arg // number type
});
/==声明=//
甲级{
福(
类型:U,
回调:(…args:U扩展T?T[U]:any]=>any
){/**一些实现*/}
}
类型T_BConstsType={
aa:[编号],
bb:[字符串]
}
B类扩展了A类{
常量:{[key in keyof T_BConstsType]:key}={
aa:‘aa’,
bb:‘bb’
}
}
//====呼叫========//
常数b=新的b;
b、 foo(b.CONSTS.aa,(arg)=>{
//这样我就可以知道“arg”是“number”类型
arg//数字类型
});