I assumed this block would do some type check for me.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 type PaymentStepEvent = PaymentStepSetup | PaymentStepDatainterface PaymentStepSetup { step: PAYMENT_STEPS.SETUP } interface PaymentStepData { step: PAYMENT_STEPS.DATA } enum PAYMENT_STEPS { DATA = 'DATA' , SETUP = 'SETUP' , }
1 2 3 4 5 6 7 let eventData: PaymentStepEventtry { eventData = JSON .parse(event.data) } catch (e) { }
The eventData: PaymentStepEvent
looked like assigning the result of JSON.parse(event.data)
to eventData
would pass some type checking, but the sad truth is not.
Execute this block in playground:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 let data = '{"dog": "DATA"}' let eventData: PaymentStepEventtry { eventData = JSON .parse(data) console .log(eventData) console .log( 'is recognized gateway message event: ' + !!isRecognizedGatewayMessageEventData(eventData) ) } catch (e) { console .log('error parsing!' ) } function isPaymentStepEvent (eventData: PaymentStepEvent ): boolean { return eventData.step && Object .values(PAYMENT_STEPS).includes(eventData.step) }
The isPaymentStepEvent
serves as a type guard, which has to be implemented by user. TypeScript adds no type checking at run time at all.