小编Mar*_*com的帖子

打字稿:如何将字符串转换为类型

我正在创建一堆 Web 组件,每个组件都可以发出自定义事件。举个例子,这里有两个简单的例子:

//MyButton
emits 'myButtonPressed' and detail of this type:
interface ButtonDetailType {
  id: string;
}

//MySlider
emits 'mySliderChanging' and details of this type:
interface SliderChangingDetailType {
  id: string;
  value: number;
}
emits 'mySliderChanged' and details of this type:
interface SliderChangedDetailType {
  id: string;
  oldValue: number;
  newValue: number;
}
Run Code Online (Sandbox Code Playgroud)

要收听组件,我的代码如下所示:

buttonEl.addEventListener( 'myButtonPressed', ( event : CustomEvent ) => {
  const detail = event.detail as ButtonDetailType;
  //now i have access to detail.id
} );

sliderEl.addEventListener( 'mySliderChanging', ( event : CustomEvent …
Run Code Online (Sandbox Code Playgroud)

types casting custom-events typescript

10
推荐指数
1
解决办法
1万
查看次数

如何将类似的Java函数合并为一个(在C++中我使用模板)

在我的一个Java类中,我有两个非常相似的函数.Java中有没有办法将它们组合成一个函数,所以我不需要维护2个函数?

public static boolean areValuesValid( double [] values, int numElements ) {
    if( values == null || values.length != numElements ) {
        return false;
    }
    for( int i = 0; i < numElements; ++i ) {
        if( Double.isNaN( values[i] ) ) {
            return false;
        }
    }
    return true;
}

public static boolean areValuesValid( float [] values, int numElements ) {
    if( values == null || values.length != numElements ) {
        return false;
    }
    for( int i = 0; i < …
Run Code Online (Sandbox Code Playgroud)

java generics function

6
推荐指数
2
解决办法
151
查看次数

如何将 ImmutableJS Map 与 TypeScript 结合使用

我有一个看起来像这样的树结构:

interface TreeData {
  id : number;
  text : string;
  children : TreeData[];
}
Run Code Online (Sandbox Code Playgroud)

我想将其包装到一个不可变的映射中,但由于我使用的是 TypeScript,所以我希望使用 get 和 set 具有一些类型安全性。我在网上找到了 2 篇文章,了解如何做到这一点的建议,请参阅:

https://github.com/facebook/immutable-js/issues/683 https://blog.mayflower.de/6630-typescript-redux-immutablejs.html

然而我的问题是我的 TreeData 包含 TreeData 子级的列表。如果我这样定义 ImmutableMap:

export interface ImmutableMap<T, K, V> extends Map<K, V> {
  get<I extends keyof T>( key : I & K ) : T[I] & V;
  set<S extends keyof T>( key : S & K, value : T[S] & V ) : Map<K, V>;
}
Run Code Online (Sandbox Code Playgroud)

然后,当我尝试定义以下内容时,我收到一条打字稿错误消息:

type ImmutableTreeData = ImmutableMap<TreeData, string, string | …
Run Code Online (Sandbox Code Playgroud)

dictionary immutability circular-reference typescript

6
推荐指数
1
解决办法
8066
查看次数

stenciljs 条件渲染返回 tsx

我的 stenciljs 渲染函数目前是用打字稿这样编写的:

render() {
  if( this._isInline ) {
    return (
      <span>
        <slot />
      </span>
    );
  } else {
    return (
      <div>
        <slot />
      </div>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

但我更喜欢如果我能写成这样:

render() {
  const tag = this._isInline ? 'span' : 'div';
  return (
    <{tag}>
      <slot />
    </{tag}>
  );
}
Run Code Online (Sandbox Code Playgroud)

但这给了我一堆错误消息。

有没有办法编写 jsx 代码,以便我有条件打开和关闭标记?

javascript render jsx tsx stenciljs

4
推荐指数
1
解决办法
4187
查看次数

打字稿检查类型A ===类型B | C型

在一个文件中,我有这样的东西:

export const _all = {
  a: '',
  b: '',
  c: '',
  d: '',
  e: '',
  f: '',
}
type AllKeysType = typeof _all;
export type AllKey = keyof AllKeysType;
Run Code Online (Sandbox Code Playgroud)

在另一个文件中,我有这样的东西:

export const _keep = {
  a: '',
  b: '',
  d: '',
  e: '',
}
type KeepKeysType = typeof _keep;
export type KeepKey = keyof KeepKeysType;

export const _ignore = {
  c: '',
  f: '',
}
type IgnoreKeysType = typeof _ignore;
export type IgnoreKey = keyof IgnoreKeysType;
Run Code Online (Sandbox Code Playgroud)

如何使用Typescript断言在_allALWAYS中定义的键始终等于 …

types assert typeof typescript keyof

4
推荐指数
1
解决办法
124
查看次数

如何在Jest中模拟函数

我有以下要在Jest中测试的打字稿类。

//MyClass.ts
import { foo } from './somewhere/FooFactory';
export class MyClass {
  private _state : number;
  constructor( arg : string ) {
    this._state = foo( arg );
  }

  public getState() : string {
    return this._state;
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我的测试:

//MyClass.spec.ts
import { MyClass } from './MyClass';
describe( 'test MyClass', () => {
  test( 'construct' => {
    const c = new MyClass( 'test' );
    expect( c ).toBeDefined();
    expect( c.getState() ).toEqual( 'TEST' );
  } );
} );
Run Code Online (Sandbox Code Playgroud)

如何模拟MyClass中使用的foo函数,以便此测试通过?

mocking typescript jestjs

3
推荐指数
1
解决办法
1951
查看次数

vuex getter,带有用Typescript编写的参数

可以创建一个带参数参数的vuex存储getter,如下所示:https ://vuex.vuejs.org/en/getters.html

我正在使用Typescript(https://github.com/hmexx/vue_typescript_starter_kit)编写我的代码,但是我不知道如何编写带有参数自变量的getter。即,以下似乎无效:

export function getItemById(state : State, id : Number) : MyItem | undefined     {
  if(id === undefined) {
    return undefined;
  }
  for(const item of state.items) {
    if(item.id === id) {
      return item;
    }
  }
  return undefined;
}

export default <GetterTree<State, any>> {
  getItemById
};
Run Code Online (Sandbox Code Playgroud)

getter arguments typescript vuex

1
推荐指数
1
解决办法
3252
查看次数

如何确定插槽中的子项数量

无论如何要知道一个命名插槽包含多少个孩子?在我的 Stencil 组件中,我的渲染函数中有这样的东西:

<div class="content">
  <slot name="content"></slot>
</div>
Run Code Online (Sandbox Code Playgroud)

我想要做的是根据插槽内有多少孩子对 div.content 进行不同的样式设置。如果插槽中没有子项,则 div.content 的 style.display='none',否则,我将一堆样式应用于 div.content,使子项正确显示在屏幕上。

我试着做:

  const divEl = root.querySelector( 'div.content' );
  if( divEl instanceof HTMLElement ) {
    const slotEl = divEl.firstElementChild;
    const hasChildren = slotEl && slotEl.childElementCount > 0;
    if( !hasChildren ) {
      divEl.style.display = 'none';
    }
  }
Run Code Online (Sandbox Code Playgroud)

但是,即使我将项目插入插槽,这也总是报告 hasChildren = false。

slots stenciljs

1
推荐指数
1
解决办法
1322
查看次数

接受 ostringstream 等参数的 c++ 函数?

C++ 有没有办法定义一个函数,这样我就可以做这样的事情:

foo( "Error on line " << iLineNumber );
foo( "name " << strName << " is Invalid" );
Run Code Online (Sandbox Code Playgroud)

就像现在一样,我必须在调用 foo 函数之前声明一个 ostringstream 对象,如下所示:

std::ostringstream strStream1;
ostringstream1 << "Error on line " << iLineNumber;
foo( ostringstream1 );

std::ostringstream strStream2;
ostringstream2 << "name " << strName << " is Invalid";
foo( ostringstream2 );
Run Code Online (Sandbox Code Playgroud)

c++ string arguments function stream

-1
推荐指数
1
解决办法
111
查看次数