openmeter / api /spec /packages /aip /lib /rules /no-nullable.js
Leon4gr45's picture
Upload folder using huggingface_hub
048b1e8 verified
Raw
History Blame Contribute Delete
738 Bytes
import { createRule, isNullType } from '@typespec/compiler'
export const noNullableRule = createRule({
name: 'no-nullable',
description: 'Use `?` for optional properties.',
severity: 'warning',
messages: {
default:
"Don't use `| null`. If you meant to have an optional property, use `?`. (e.g. `myProp?: string`)",
},
create(context) {
return {
modelProperty: (property) => {
if (property.type.kind !== 'Union') {
return
}
if (
!property.node?.optional &&
[...property.type.variants.values()].some((x) => isNullType(x.type))
) {
context.reportDiagnostic({
target: property,
})
}
},
}
},
})