diff --git a/types/geojson/geojson-tests.ts b/types/geojson/geojson-tests.ts index ce8c62418e..19a3a4fd53 100644 --- a/types/geojson/geojson-tests.ts +++ b/types/geojson/geojson-tests.ts @@ -248,3 +248,77 @@ const typedPropertiesFeatureCollection: FeatureCollection = { type: "FeatureCollection", features: [typedPropertiesFeature] }; + +// Strict Null Checks + +let isNull: null; +let isPoint: Point; +let isPointOrNull: Point | null; +let isProperty: TestProperty; +let isPropertyOrNull: TestProperty | null; + +const featureAllNull: Feature = { + type: "Feature", + properties: null, + geometry: null +}; + +const featurePropertyNull: Feature = { + type: "Feature", + properties: null, + geometry: point +}; + +const featureGeometryNull: Feature = { + type: "Feature", + properties: testProps, + geometry: null +}; + +const featureNoNull: Feature = { + type: "Feature", + properties: testProps, + geometry: point +}; + +const collectionAllNull: FeatureCollection = { + type: "FeatureCollection", + features: [featureAllNull], +}; + +const collectionMaybeNull: FeatureCollection = { + type: "FeatureCollection", + features: [featureAllNull, featurePropertyNull, featureGeometryNull, featureNoNull], +}; + +const collectionPropertyMaybeNull: FeatureCollection = { + type: "FeatureCollection", + features: [featurePropertyNull, featureNoNull], +}; + +const collectionGeometryMaybeNull: FeatureCollection = { + type: "FeatureCollection", + features: [featureGeometryNull, featureNoNull], +}; + +const collectionNoNull: FeatureCollection = { + type: "FeatureCollection", + features: [featureNoNull], +}; + +isNull = featureAllNull.geometry; +isPoint = featurePropertyNull.geometry; +isNull = featureAllNull.properties; +isProperty = featureGeometryNull.properties; + +isNull = collectionAllNull.features[0].geometry; +isPointOrNull = collectionMaybeNull.features[0].geometry; +isPoint = collectionPropertyMaybeNull.features[0].geometry; +isPointOrNull = collectionGeometryMaybeNull.features[0].geometry; +isPoint = collectionNoNull.features[0].geometry; + +isNull = collectionAllNull.features[0].properties; +isPropertyOrNull = collectionMaybeNull.features[0].properties; +isPropertyOrNull = collectionPropertyMaybeNull.features[0].properties; +isProperty = collectionGeometryMaybeNull.features[0].properties; +isProperty = collectionNoNull.features[0].properties; diff --git a/types/geojson/index.d.ts b/types/geojson/index.d.ts index 85f02aaad0..b1c7b9f39a 100644 --- a/types/geojson/index.d.ts +++ b/types/geojson/index.d.ts @@ -139,12 +139,12 @@ export type GeoJsonProperties = { [name: string]: any; } | null; * A feature object which contains a geometry and associated properties. * https://tools.ietf.org/html/rfc7946#section-3.2 */ -export interface Feature extends GeoJsonObject { +export interface Feature extends GeoJsonObject { type: "Feature"; /** * The feature's geometry */ - geometry: G | null; + geometry: G; /** * A value that uniquely identifies this feature in a * https://tools.ietf.org/html/rfc7946#section-3.2. @@ -153,14 +153,14 @@ export interface Feature extend /** * Properties associated with this feature. */ - properties: P | null; + properties: P; } /** * A collection of feature objects. * https://tools.ietf.org/html/rfc7946#section-3.3 */ -export interface FeatureCollection extends GeoJsonObject { +export interface FeatureCollection extends GeoJsonObject { type: "FeatureCollection"; features: Array>; } diff --git a/types/geojson/tsconfig.json b/types/geojson/tsconfig.json index 071f17c65d..86021d5757 100644 --- a/types/geojson/tsconfig.json +++ b/types/geojson/tsconfig.json @@ -6,7 +6,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [