Upgrade to Pro — share decks privately, control downloads, hide ads and more …

interface を活用して安全性と拡張性を兼ね備えた型を作る

all-user
November 26, 2024

interface を活用して安全性と拡張性を兼ね備えた型を作る

2024/11/16 TSKaigi KansaiのLTで使用した資料です!
https://kansai.tskaigi.org/talks/all-user

all-user

November 26, 2024
Tweet

Other Decks in Programming

Transcript

  1. © Techtouch, Inc. 11 ϝοηʔδͷܕΛͲͷΑ͏ʹදݱ͢Δ͔ // ϥΠϒϥϦଆ: @tt/messaging export type

    Message = { type: string; // ܕ҆શੑ͕ࣦΘΕΔ payload: any; // ܕ҆શੑ͕ࣦΘΕΔ }; const createMessaging = () => { return { postMessage(type: string, payload: any) { // ࣮૷ }, }; };
  2. © Techtouch, Inc. 12 ۩ମతͳ༻్͸ΞϓϦέʔγϣϯଆ͔͠஌Βͳ͍ͷͰ େ·͔ͳܕ͔͠ఆٛ͢Δ͜ͱ͕Ͱ͖ͳ͍ɻ // ϥΠϒϥϦଆ: @tt/messaging export

    type Message = { type: string; // ܕ҆શੑ͕ࣦΘΕΔ payload: any; // ܕ҆શੑ͕ࣦΘΕΔ }; const createMessaging = () => { return { postMessage(type: string, payload: any) { // ࣮૷ }, }; };
  3. © Techtouch, Inc. 13 // ϥΠϒϥϦଆ: @tt/messaging export type Message

    = | { type: "scrollTo"; payload: { x: number; y: number; }; } | { type: "dispatchInput"; payload: { value: string; }; } const createMessaging = () => { return { send<Msg extends Message>(type: Msg["type"], payload: Msg["payload"]) { // ࣮૷ }, }; }; ͡Ό͋ϥΠϒϥϦଆʹશ෦ॻ͍ͪΌ͓͏
  4. © Techtouch, Inc. 14 // ϥΠϒϥϦଆ: @tt/messaging export type Message

    = | { type: "scrollTo"; payload: { x: number; y: number; }; } | { type: "dispatchInput"; payload: { value: string; }; } | { type: "getBoundingClientRect"; payload: { selector: string; }; }; const createMessaging = () => { return { send<Msg extends Message>(type: Msg["type"], payload: Msg["payload"]) { // ࣮૷ }, }; }; ΞϓϦέʔγϣϯଆͰͷ༻్͕૿͑Δͨͼʹ ϥΠϒϥϦଆʹखΛՃ͑Δඞཁ͕͋Δ
  5. © Techtouch, Inc. 15 // ϥΠϒϥϦଆ: @tt/messaging export type Message

    = | { type: "scrollTo"; payload: { x: number; y: number; }; } | { type: "dispatchInput"; payload: { value: string; }; } | { type: "getBoundingClientRect"; payload: { selector: string; }; }; const createMessaging = () => { return { send<Msg extends Message>(type: Msg["type"], payload: Msg["payload"]) { // ࣮૷ }, }; }; ผͷΞϓϦέʔγϣϯͰར༻͢Δͱ͍ͬͨ ࠶ར༻ੑ΋٘ਜ਼ʹͳͬͯ͠·͏
  6. © Techtouch, Inc. 19 declare module '@mui/material/styles' { interface Theme

    { status: { danger: string; }; } // allow configuration using `createTheme()` interface ThemeOptions { status?: { danger?: string; }; } } 5IFNF΍5IFNF0QUJPOTͷܕΛ ͋ͱ͔Β֦ுͰ͖Δ
  7. © Techtouch, Inc. 21 // ϥΠϒϥϦଆ: @tt/messaging export interface MessageMap

    { // ϥΠϒϥϦଆͰ͸Կ΋ఆٛ͠ͳ͍ } const createMessaging = () => { return { send<T extends keyof MessageMap>(type: T, payload: MessageMap[T]) { // ࣮૷ }, }; }; ϥΠϒϥϦଆͰ͸ඞཁ࠷௿ݶͷఆٛͷΈ
  8. © Techtouch, Inc. 22 // ϥΠϒϥϦଆ: @tt/messaging export interface MessageMap

    { // ϥΠϒϥϦଆͰ͸Կ΋ఆٛ͠ͳ͍ } const createMessaging = () => { return { send<T extends keyof MessageMap>(type: T, payload: MessageMap[T]) { // ࣮૷ }, }; }; // ΞϓϦέʔγϣϯଆ: app.ts declare module "@tt/messaging" { interface MessageMap { scrollTo: { x: number; y: number; }; } } const messaging = createMessaging(); messaging.send("scrollTo", { x: 10, y: 42, }); // ✅ ܕ҆શ ΞϓϦέʔγϣϯଆͰ͸ ༻్ʹԠͨ͡ϝοηʔδͷܕΛఆٛ ϥΠϒϥϦଆͰ͸ඞཁ࠷௿ݶͷఆٛͷΈ
  9. © Techtouch, Inc. 24 ΞϓϦέʔγϣϯଆͰ͜ΕΒͷએݴΛ ҰՕॴʹ·ͱΊͯهड़͢Δඞཁ͸ͳ͍ declare module "@tt/messaging" {

    interface MessageMap { scrollTo: { x: number; y: number; }; } interface MessageMap { dispatchInput: { value: string; }; } interface MessageMap { getBoundingClientRect: { selector: string; }; } }
  10. © Techtouch, Inc. 25 ͦͷͨΊɺΞϓϦέʔγϣϯଆͰ͜ΕΒͷએݴΛ ҰՕॴʹ·ͱΊͯهड़͢Δඞཁ͸ͳ͍ declare module "@tt/messaging" {

    interface MessageMap { scrollTo: { x: number; y: number; }; } } declare module "@tt/messaging" { interface MessageMap { dispatchInput: { value: string; }; } } declare module "@tt/messaging" { interface MessageMap { getBoundingClientRect: { selector: string; }; } }
  11. © Techtouch, Inc. 26 ؔ࿈͢Δ࣮૷ͷ͙ۙ͘͢Ͱએݴ ϘτϜΞοϓʹܕΛఆٛ declare module "@tt/messaging" {

    interface MessageMap { scrollTo: { x: number; y: number; }; } } declare module "@tt/messaging" { interface MessageMap { dispatchInput: { value: string; }; } } declare module "@tt/messaging" { interface MessageMap { getBoundingClientRect: { selector: string; }; } }
  12. © Techtouch, Inc. 27 // ΞϓϦέʔγϣϯଆ: iframeScript.ts import { scrollTo

    } from "./PATH_TO/scrollTo.ts"; import { dispatchInput } from "./PATH_TO/dispatchInput.ts"; import { getBoundingClientRect } from "./PATH_TO/getBoundingClientRect.ts"; export const iframeImplements = { scrollTo, dispatchInput, getBoundingClientRect, } satisfies Record<keyof MessageMap, unknown>; એݴϚʔδ͞Εͨ࠷ऴతͳܕΛར༻ͯ͠ ܕνΣοΫΛߦ͏
  13. © Techtouch, Inc. 28 એݴϚʔδ͞Εͨ࠷ऴతͳܕΛར༻ͯ͠ ܕνΣοΫΛߦ͏ // ΞϓϦέʔγϣϯଆ: iframeScript.ts import

    { scrollTo } from "./PATH_TO/scrollTo.ts"; import { dispatchInput } from "./PATH_TO/dispatchInput.ts"; // import { getBoundingClientRect } from "./PATH_TO/getBoundingClientRect.ts"; export const iframeImplements = { scrollTo, dispatchInput, // getBoundingClientRect, // ^ ૊ΈࠐΉͷΛ๨Εͯ͠·ͬͨɺɺɺ } satisfies Record<keyof MessageMap, unknown>; // ❌ શͯͷϓϩύςΟ͕ἧ͍ͬͯΔ͜ͱΛݕࠪͰ͖Δ