Share
Embed
Transcript
16 slides
-
+40/ͱ ʢೖฤʣ w +BWB4DSJQU0CKFDU/PUBUJPO w +BWB4DSJQUͷΦϒδΣΫ τ ɾ Ϧςϥϧͱ͍͠දه͕ՄೳͳߏԽσʔλఆٛܗࣜ w
package.jsontsconfig.jsonͳͲઃఆϑΝΠϧʹ࠾༻͞Ε͍ͯΔͨΊ5ZQF4DSJQU։ൃʹ ͓͍ͯɺ ΄΅શһ͕ʹ͢Δ w +BWB4DSJQUҎ֎ʹ1ZUIPO 1)1 3VCZ (PͳͲ෯͍ϓϩάϥϛϯάݴޠͰ+40/ͷม͕Մೳ Ͱ͋Δ -
+40/ͱ ʢྺ࢙ฤʣ w ɺ ॳظͷ8FCϒϥβ/FUTDBQF/BWJHBUPSʹࡌ͞ΕͨεΫ Ϧ ϓτݴޠ +BWB4DSJQU چ-JWF4DSJQU
w ओཁ։ൃऀ#SFOEBO&JDIࢯ w ͜ͷݴޠʹඋΘ͍ͬͯͨΦϒδΣΫ τ ɾ Ϧςϥϧͷ࣌Ͱطʹ/FUTDBQF/BWJHBUPSͷ σʔλަʹ࠾༻͞Ε͍ͯͨ w ͦͷޙඪ४ԽΛܦͨ&$."4DSJQUͷΦϒδΣΫ τ ɾ ϦςϥϧΛ༻͍ͨσʔλߏදهʹର͠ ʹ%PVHMBT$SPDLGPSEࢯ͕͜ΕΛ+40/ͱ໋໊ w "KBY "TZODISPOPVT+BWB4DSJQU"OE9.- ʹ͓͍ͯ9.-Α Γ ܰྔͰ͋Δ͜ͱ͔Β ޙҎ߱ɺ +40/͕ීٴ͍ͯ͘͠ -
༷͕૬͍࣍Ͱੜ w ݄ʹ*&5'ʹͯ3'$͕ެ։ w ݄ʹ&$."UI &4 ͕ެ։ w JSON.parse(), JSON.stringify()͕
ʢҰ෦ͷྫ֎ΛؚΉ͕ɺ ΄΅ʣ 3'$ʹ४ڌ͠࠾༻ w ݄ʹ&$."5IF+40/%BUB*OUFSDIBOHF'PSNBUTU͕ެ։ w ݄ʹ&$."UI &4 ͕ެ։ w 3'$͕ґଘ͔Βআɺ طଘͷ+40/ʹؔ͢Δ࣮&$."ʹ४ڌ -
༷ͷ౷Ұ w ݄ɺ *&5'ʹͯ3'$͕ެ։ɺ &$."ʹͯ&$."OE͕ެ։ w ޓ͍ͷ༷ڞ௨Խ͞Εɺ ࣄ্࣮+40/ʹ͓͚Δ།Ұͷ༷ͱݺͼ͏ Δঢ়ଶ͕ͬͨ JSON
was first presented to the world at the JSON.org website in 2001. A definition of the JSON syntax was subsequently published as IETF RFC 4627 in July 2006. ECMA-262, Fifth Edition (2009) included a normative specification of the JSON grammar. This specification, ECMA-404, replaces those earlier definitions of the JSON syntax. Concurrently, the IETF published RFC 7158/7159 and in 2017 RFC 8259 as updates to RFC 4627. The JSON syntax specified by this specification and by RFC 8259 are intended to be identical. https: / / www.ecma-international.org/wp-content/uploads/ECMA-404_2nd_edition_december_2017.pdf -
+40/ͷεΩʔϚ w +40/ͷ༷ɺ ͋͘ ·ͰσʔλߏఆٛʹඞཁͳߏจΛఆΊ͍ͯΔʹ͗͢ͳ͍ w ͦͷதͷσʔλͻͱͭͻͱͭͷ༷ͦͷ+40/࡞ऀʹҕͶΒΕΔ w ͨͱ͑͜Μͳใ w
ͦͷɺ ԿจࣈҎ্Ͱɺ ԿจࣈҎԼͷจࣈྻͰ͋Δ w ͦͷɺ ྻڍ͞ΕͨϓϩύςΟ ΛඞؚͣΉΦϒδΣΫ τͰ͋Δ w ͦͷɺ ಛఆͷ୯ޠͷΈͰߏ͞ΕΔྻͰ͋Δ w +40/ʹର͢Δ͜ͷΑ ͏ͳεΩʔϚใΛѻ͍͍ͨͱ͍͏ཉٻ͕ੜ·Ε࢝ΊΔ -
+40/4DIFNB w +40/ͰѻΘΕΔϓϩύςΟͷߏ ɾ ܕΛఆٛ͢ΔͨΊͷܗࣜΛ+40/4DIFNBͱ͍͏ w ҙͷ+40/ͷσʔλ༷Λ ʮ+40/4DIFNBͱ͍͏+40/ʯ Ͱදݱ͢Δ w
+40/4DIFNBͭͷ༷ʹΑ ΓΓཱ͍ͬͯΔ w +40/4DIFNB$PSF+40/4DIFNBͷجૅͱͳΔఆٛ w +40/4DIFNB7BMJEBUJPO֦ு༷ͱͯ͠όϦσʔγϣϯʹؔ͢Δࣄ߲ͷఆٛ -
+40/4DIFNBͷ8FCඪ४Խ ʁ w +40/4DIFNBͷ൛ESBGUESBGUͷΑ ͏ʹESBGU಄͕ࣙ͘ w ͜Ε*&5'ʹର͢ΔESBGUͱ͍͏ҙຯͰ͋Γ BKWͳͲͷ+40/4DIFNBΛղऍ͢ΔόϦσʔγϣϯ ɾ
ϥΠ ϒϥ Ϧɺ ࣄ্࣮͜ΕΛਖ਼نͷ༷ͱͯ͠ ѻ͍ͬͯΔ w ESBGU͔ͩΒͱͯ͠ෆ҆ఆͱ͍͏ҙຯͰͳ͘ɺ ܗࣜతʹESBGU಄͕͍͍ࣙͭͯΔঢ়گ w KTPOTDIFNBPSH༗ࢤͷஂମͰ͋Γ*&5'ͷϫʔΩϯάάϧʔϓͰͳ͍ w Αͬͯ*&5'ඪ४Խϓϩηε͔Βͷ٫Λਤ͍ͬͯΔ༷ https: / / github.com/json-schema-org/json-schema-spec/blob/main/adr/2022-09-decouple-from-ietf.md -
8FCඪ४Խ͍ͯ͠ͳ͍ͷΛ҆৺ͯ͑͠Δ͔ ʁ w +40/4DIFNBʹ͍ͭͯͷٞͱվగͷܾఆͯ͢Φʔϓϯʹͳ͍ͬͯΔ w ͢Ͱʹ͔Βͷ༷ͷੵͱීٴʹΑ Γɺ ੈքதͷ։ൃऀ͔Βؔ৺͕دͤΒΕ͍ͯΔ w *&5'ඪ४Խϓϩηεʹ؇͔ʹଇΓESBGUΛॏͶ͖ͯͨ͜ͱ͔Βɺ
༷ॻࣗମͷ࣭͓͟ͳΓͰͳ͍ w 8FCඪ४Խ͞Εͣʹ8FC։ൃपลʹͯීٴٕͨ͠ज़ଞʹ͍ͭ͋͘Δ w ӦརஂମʹΑͬͯ࡞͞ΕσϑΝΫ τελϯμʔ υͱͯ͠ීٴͨ͠0(1ɺ ઐ༻ͷඇӦརஂମʹΑͬͯཧ͞ΕΔ.BSLEPXO :".-ͳͲ -
KTPOTDIFNBUPUT w ;PE͕ྲྀߦ͍ͯ͠Δ w ;PE5ZQF4DSJQU༻ϥΠ ϒϥ ϦͰ͋Γɺ ܕఆٛੜ͓Αͼ3VOUJNF7BMJEBUPSͷఏڙΛߦ͏ w ࣮+40/4DIFNBΛ͍ͬͯۙ͜ͱ͕Ͱ͖Δ
w KTPOTDIFNBUPUTͱ͍͏ ϥΠ ϒϥ ϦΛ͏ https: // github.com/ThomasAribart/json-schema-to-ts#readme w FromSchema<typeof schema>ͷΑ ͏ʹͯ͠+40/4DIFNBΛදݱͨ͠ΦϒδΣΫ τ ɾ Ϧςϥϧ Λ֨ೲ͢ΔมͷܕΛtypeof͖Ͱ͢ͱɺ +40/4DIFNBʹج͍ͮͨܕΛੜ͢Δ w όϦσʔγϣϯॲཧʹBKWΛ͏ -
͍ํ const schema1 = { $schema: 'http://json-schema.org/draft-07/schema#', type: 'string', }
as const satisfies JSONSchema7; const schema2 = { $schema: 'http://json-schema.org/draft-07/schema#', type: 'null', } as const satisfies JSONSchema7; type T1 = FromSchema<typeof schema1>; type T2 = FromSchema<typeof schema2>; assertType<T1, string>(true); assertType<T2, null>(true); w as const satisfies JSONSchema7 Λ͏ ͱิ͕ޮ͍ͯศར w type: 'string'ͷ4DIFNB͔Β stringܕ͕ಘΒΕΔ -
const schema = { $schema: 'http://json-schema.org/draft-07/schema#', type: 'object', properties: {
foo: { type: 'number' }, bar: { type: 'boolean' }, }, } as const satisfies JSONSchema7; type T = FromSchema<typeof schema>; assertType< T, { [x: string]: unknown; foo?: number; bar?: boolean; } >(true); const schema = { $schema: 'http://json-schema.org/draft-07/schema#', type: 'object', additionalProperties: false, properties: { foo: { type: 'number' }, bar: { type: 'boolean' }, }, required: ['foo'], } as const satisfies JSONSchema7; type T = FromSchema<typeof schema>; assertType< T, { foo: number; bar?: boolean; } >(true); w requiredadditionalProperties͕ࢦఆ͞Ε͍ͯͳ͍ w [x: string]: unknown;0QUJPOBMؚ͕·ΕΔ w additionalProperties: falseͩͱ[x: string]: unknownࢦఆؚ·Εͳ͍ w requiredʹج͍ͮͯ0QUJPOBM͕֎ΕΔ -
w ྻѻ͏ ͜ͱ͕Ͱ͖Δ w 6OJPOܕͷදݱʹoneOfΛ༻͍Δ const schema = { $schema:
'http://json-schema.org/draft-07/schema#', type: 'array', items: { oneOf: [ { type: 'string' }, { type: 'object', properties: { foo: { type: 'number' } } }, ], }, } as const satisfies JSONSchema7; type T = FromSchema<typeof schema>; assertType< T, ( | string | { [x: string]: unknown; foo?: number; } )[] >(true); -
੯͍͠ͱ ͜Ζ w $refʹରԠ͍ͯ͠ΔͷͰ֎෦εΩʔϚಡΊΔ w ϑΝΠϧΛ͑ͨ͋ͨΓͰ5ZQF4DSJQUίϯύΠ ϥͷ࠶ؼͷݶքʹḷΓண͍ͨͷͰ $refલఏͷεΩʔϚࡉԽΞʔΩςΫνϟͩͱ٧Ή w
ྻͷόϦσʔγϣϯ༻ଐੑminItems, maxItemsͳͲΛઃఆͯ͠ string[]͕[string, string, string]ʹͳͬͨΓ͠ͳ͍ w ࡉ͔͍ͱ͜Ζࠓޙʹظ͍ͨ͠ -
$PODMVTJPO w +40/4DIFNBΨϯΨϯॻ͍ͯϤγ w όϦσʔγϣϯ ɾ ϥΠ ϒϥ ϦͰ͋ΔBKWΛͬͯ+40/4DIFNBΛݩʹͨ͠όϦσʔγϣϯΛ࣮ߦ w
KTPOTDIFNBUPUTΛͬͯܕใ+40/4DIFNB͔Βੜ ˠ+40/4DIFNB͍͍ͧ