LoginSignup
0
0

More than 1 year has passed since last update.

typescriptのtypeと同じような動きをpythonで実装したい。

Posted at

以下のようにObjectに型をtypescriptでつけることがある。これをpythonでも同じようにやりたい。

type ProfileInterface = {
    name: string
    title: string
    email: string
}

const profile_objects: ProfileInterface = {
    name: "test",
    title: "mamushi",
    email: "test@hoge.com"
}

【解決策】TypedDictを使用する。

from typing import TypedDict

class ProfileInterface(TypedDict):
    name: str
    title: str
    email: str


profile_objects: ProfileInterface = {
    "name": "mamushi",
    "title": "typescriptのinterfaceみたいに書きたい。",
    "email": "test@hoge.com",
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0