Exploring the intricacies of the : never
type in TypeScript can lead to a deeper understanding of discriminated unions, a more advanced concept in the language.
While this explanation may not cover everything, it serves as a hint to help grasp the issue at hand and the potential limitations with string return values.
type O = {
name: string
city: string
}
declare const neverValue: never;
// Uncommenting this will result in failure
// function returnStringS(s: string): 's' {
// return 's';
// }
// These examples will work without any issues
function returnStringS(s: string): string {
return 's';
}
function returnStringS(s: string): string {
return s;
}
let o1: O = {
name: "Marc",
city: "Paris",
[returnStringS("random")]: "London",
}
let o2: O = {
name: "Marc",
city: "Paris",
[returnStringS("random")]: "London",
}
const some2: O = {
name: "Marc",
city: "Paris",
[neverValue]: "London"
}
For further insight into the TypeScript never type and its implications, refer to the documentation on TS never type.
The never type is a subtype of every type but no type is a subtype of never (except never itself). Even any type isn't assignable to never.