#Strange invalid, but working syntax: property: ?type; resolving to type | null

7 messages · Page 1 of 1 (latest)

proud island
#

Has anyone ever come across this syntax before?

interface User {
  id: ?string;
}

This^ is specifically resolving to string | null, but it's not legal TS?

This only works in .d.ts files, and in regular .ts files it appropriately has the error.

ChatGPT seems to think that d.ts files are more loose and can support older features of TS, but haven't found any info online that actually supports this.

elfin yacht
#

@proud island Do you have skipLibCheck: true? If so, .d.ts files aren't type-checked and errors aren't reported.

proud island
#

i do, but why would it infer it as | null

elfin yacht
#

It's a legal JSDoc type, apparently.

#

So TS probably has some codepath for handling it, since it supports JSDoc types in .js files, but reports the error if you actually use it outside of JSDoc.

tidal shadowBOT
#
interface User {
    id: ?string;
//      ^^^^^^^
// '?' at the start of a type is not valid TypeScript syntax. Did you mean to write 'string | null | undefined'?
}
#
// @filename: example.js

/** @param {?string} bar */
function foo(bar) {
//            ^? - (parameter) bar: string | null
}