面试题答案
一键面试在TypeScript项目中,与GraphQL基本标量类型相对应的TypeScript类型定义如下:
- String:在TypeScript中直接使用
string
类型。例如,GraphQL中的String
类型字段,在TypeScript中可以这样定义接口:
interface User {
name: string;
}
- Int:在TypeScript中使用
number
类型,因为JavaScript中没有专门的整数类型。例如:
interface Post {
likes: number;
}
- Float:同样在TypeScript中使用
number
类型。示例:
interface Product {
price: number;
}
- Boolean:在TypeScript中使用
boolean
类型。如:
interface Task {
completed: boolean;
}
- ID:通常在TypeScript中也使用
string
类型来表示,因为GraphQL的ID
本质上是一个唯一标识符,一般以字符串形式存在。例如:
interface Comment {
id: string;
}
通过这样的类型定义,可以确保数据在TypeScript与GraphQL之间转换时类型的一致性。