不能 'convert' 在 javascript 中定义打字稿

can't 'convert' typescript definition in javascript

我正在尝试将 this Apollo typescript repository 重写为 javascript。 我尝试学习打字稿,但有一种叫做 'type casting' 或 'type assertion' 的东西让我感到困惑。

我用代码解释一下:

//cache.tsx

import { InMemoryCache, ReactiveVar, makeVar } from "@apollo/client";
import { Todos } from "./models/Todos";
import { VisibilityFilter, VisibilityFilters } from "./models/VisibilityFilter";

export const cache...{
...
...
}


export const todosVar: ReactiveVar<Todos> = makeVar<Todos>( //how i can 'convert' this type caster value to vanilla js?
  todosInitialValue 
);

export const visibilityFilterVar = makeVar<VisibilityFilter>(  //how i can 'convert' this type caster value to vanilla js?
  VisibilityFilters.SHOW_ALL
)

此 cache.tsx 使用的其他 2 个文件是:

//todos.tsx

export interface Todo {
  text: string;
  completed: boolean;
  id: number
}

export type Todos = Todo[];

VisibilityFilter.tsx


export type VisibilityFilter = {
  id: string;
  displayName: string;
}

export const VisibilityFilters: { [filter: string]: VisibilityFilter } = {
  SHOW_ALL: {
    id: "show_all",
    displayName: "All"
  },
  SHOW_COMPLETED: {
    id: "show_completed",
    displayName: "Completed"
  },
  SHOW_ACTIVE: {
    id: "show_active",
    displayName: "Active"
  }
}

在这种情况下如何避免打字稿类型检查,更重要的是,如何正确使用 ReactiveVar 和 makeVar 导入?

像这样删除泛型:

发件人:

makeVar<Todos>(

收件人:

makeVar(

改变这个:

export const todosVar: ReactiveVar<Todos> = makeVar<Todos>( //how i can 'convert' this type caster value to vanilla js?
  todosInitialValue 
);

export const visibilityFilterVar = makeVar<VisibilityFilter>(  //how i can 'convert' this type caster value to vanilla js?
  VisibilityFilters.SHOW_ALL
)

为此:

export const todosVar = makeVar(todosInitialValue);
export const visibilityFilterVar = makeVar(VisibilityFilters.SHOW_ALL);

如何避免打字稿检查 - 分别将您的扩展名从 .ts 和 .tsx 替换为 .js 和 .jsx。