Comments (4)
Sorry for delay, I missed your message.
RTTI means RunTime Type Information. This is an object that describe how your object should be.
With it, you can check if a resource has the desired shape.
It also means RTTI can't be unknown, you should always know what type you are expecting.
for example, does my any object is an IObservation ?
RTTI_Observation.decode(**any**) // this does the job
if your any content json fits an observation, then your response should be 'right', 'left' otherwise.
If you are looking for interfaces, so you can use Ixxx ( IObservation, IPatient, ... ).
I guess you're issue is to know what kind of resource you are using. It's true that an helper method could be present is my library, specially for ResourceList type that merge all FHIR type.
This is where typeguards can be useful. If your not familiar with this part of typescript, I let you learn more about it. Here is a small sample
// Here an interface definition
interface IA {
foo: 'IA';
bar: string;
}
// Here is a typeguard for IA
function isIA( json: any): json is IA {
return json.foo === 'IA'
}
// Here is a an any definition. I forced any to prevent typescript to infer 'a' as { foo: string, bar: string }
const a: any = {
foo: 'IA',
bar: "A"
}
// Here another any type
const b: any = {
foo: 'IB',
other: "B"
}
// Let's test is a an IA
if ( isIA(a) ) {
// You can see here that a is now type IA and IntelliSense works well with types.
console.log( a.bar )
} else {
console.log( 'a is not IA')
}
// Let's do the same with b
// b can't be considered as IA because foo is not of type 'IA' ( Like FHIR 'resourceType' property does )
if ( !isIA(b) ) {
console.log( 'b is not an IA')
} else {
console.log( 'b is IB')
}
Hope this helps.
:)
from typescript-fhir-types.
Thanks for the response! Just to clarify, there is no current support in your library for doing RTTI when the FHIR resource type is unknown? i.e. you always need to call the specific interface (like your RTTI_Observation example)
from typescript-fhir-types.
the library is intended to validate a json as FHIR resource shape.
Generally speaking, you know what that shape is intended to be because you are interrogating a service in purpose.
If you ask for Foo you shouldn't receive Bar. There are specifications for service you use.
The goal of the library is to enforce the typings. meaning if you receive something in json format, it may have a wrong shape because of a bug, typo, wrong spec, etc.
Using a decode method allows you to safely assume an object is right at runtime.
if you really know nothing about your data, there is 2 ways:
- you can use
RTTI_ResourceList.decode()
, if your result is right then it means it's a valid FHIR resource but you don't know what resource. - If you want to know exactly what resource then you need to test all FHIR resources like this:
// define your typeguard
function isObservation( data: any): data is IObservation {
const result = RTTI_Observation.decode(data);
return E.isRight(validationResult) !== undefined; // I didn't test the code. but this is the main idea. If you have a valid resource you have a valid type
}
function isPatient( data: any): data is IPatient {
const result = RTTI_Patient.decode(data);
return E.isRight(validationResult) !== undefined; // I didn't test the code. but this is the main idea. If you have a valid resource you have a valid type.
}
const data: any = _your_data_here_
if( isPatient(data) ) { // typeguard
// safely access patient here
} else if( isObservation(data) ) { // typeguard
// safely access Observation here
} etc etc
You need typeguard for all resources. It's something I should add in future release.
from typescript-fhir-types.
Thank you. This answers my question
from typescript-fhir-types.
Related Issues (20)
- Support for 4.0.1 HOT 5
- Code handling for AuditEvent HOT 7
- Change to MIT Licence HOT 4
- Questionnaire_EnableWhenOperatorKind doesn't match FHIR R4 spec HOT 2
- Reject additional properties HOT 4
- RTTI_Patient not decoding birthDate HOT 2
- Question: How did you generate all the interfaces? HOT 1
- AuditEvent.recorded is not mandated HOT 2
- Why are the enum entries prefixed with underscores ? HOT 2
- Help using the package. HOT 2
- Object creation HOT 2
- Including enums brings tons of unnecessary code into the bundle HOT 3
- Support for FHIR R4B (4.3.0)
- RTTI_Location_Position - longitude and latitude should be required
- Fhir enums are a little bit annoying ... HOT 1
- Problem with RTTI esModule called non object - node 18 - vite 4.0.4, rollup 3+
- Run tests with Travis CI HOT 1
- Reconnect Scalars to Resources HOT 1
- Update Library with new definition in schema.json HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typescript-fhir-types.