top | item 45885230 (no title) mpoteat | 3 months ago Actually, it's even simpler: you should just be able to use signature overloading: myFunc(x: true): number myFunc(x: false): string The article's assertion that TypeScript can't represent this is quite false. discuss order hn newest codebje|3 months ago Alas, no: foo.ts:1:10 - error TS2393: Duplicate function implementation. wk_end|3 months ago Like this:https://www.typescriptlang.org/play/?#code/GYVwdgxgLglg9mABA...It's a bit of a hack, though - if you incorrectly implement myFunc so that, say, it returns a string even when x is true, it can violate the overloading assertions without any error or warning. load replies (1)
codebje|3 months ago Alas, no: foo.ts:1:10 - error TS2393: Duplicate function implementation. wk_end|3 months ago Like this:https://www.typescriptlang.org/play/?#code/GYVwdgxgLglg9mABA...It's a bit of a hack, though - if you incorrectly implement myFunc so that, say, it returns a string even when x is true, it can violate the overloading assertions without any error or warning. load replies (1)
wk_end|3 months ago Like this:https://www.typescriptlang.org/play/?#code/GYVwdgxgLglg9mABA...It's a bit of a hack, though - if you incorrectly implement myFunc so that, say, it returns a string even when x is true, it can violate the overloading assertions without any error or warning. load replies (1)
codebje|3 months ago
wk_end|3 months ago
https://www.typescriptlang.org/play/?#code/GYVwdgxgLglg9mABA...
It's a bit of a hack, though - if you incorrectly implement myFunc so that, say, it returns a string even when x is true, it can violate the overloading assertions without any error or warning.