[typescript] interface和type有什么关系?
type 和interface
即使不知道他们的区别,也是可以学习编程的。不要因为搞不懂type和interface的区别,而丧失了对编程的兴趣。
import type {XYZ} from ‘xxxx’
一般情况type和interface都很类似。
Both can be used to describe the shape of an object or a function signature. But the syntax differs.
https://blog.logrocket.com/types-vs-interfaces-typescript/
?:可选属性,?:说明这个属性可以不存在
errorMessage?: string;
最近有typescript和python代码互转的需求。
但是会发现除了基本的逻辑流程的转写,还涉及到数据结构的转写。而且数据结构的转写比逻辑的的转写有时候还要烧脑。
比如下面的typescript怎么转写成python的数据类型?
// Conversation tracking types for maintaining context across interactionsexport interface ConversationMessage {id: string;role: 'user' | 'assistant';content: string;timestamp: number;metadata?: {editedFiles?: string[]; // Files edited in this interactionaddedPackages?: string[]; // Packages added in this interactioneditType?: string; // Type of edit performedsandboxId?: string; // Sandbox ID at time of message};
}export interface ConversationEdit {timestamp: number;userRequest: string;editType: string;targetFiles: string[];confidence: number;outcome: 'success' | 'partial' | 'failed';errorMessage?: string;
}export interface ConversationContext {messages: ConversationMessage[];edits: ConversationEdit[];currentTopic?: string; // Current focus area (e.g., "header styling", "hero section")projectEvolution: {initialState?: string; // Description of initial project statemajorChanges: Array<{timestamp: number;description: string;filesAffected: string[];}>;};userPreferences: {editStyle?: 'targeted' | 'comprehensive'; // How the user prefers editscommonRequests?: string[]; // Common patterns in user requestspackagePreferences?: string[]; // Commonly used packages};
}export interface ConversationState {conversationId: string;startedAt: number;lastUpdated: number;context: ConversationContext;
}```