TypeScript/tests/baselines/reference/taggedPrimitiveNarrowing.js
Anders Hejlsberg d41943eb4e
Properly handle tagged primitives in control flow analysis (#43538)
* Ignore object types in intersections with primitive types

* Add regression test

* Also handle instantiable types constrained to object types

* Add another test

* Add ignoreObjects optional parameter to getTypeFacts
2021-04-07 11:29:46 -10:00

43 lines
1.1 KiB
TypeScript

//// [taggedPrimitiveNarrowing.ts]
type Hash = string & { __hash: true };
function getHashLength(hash: Hash): number {
if (typeof hash !== "string") {
throw new Error("This doesn't look like a hash");
}
return hash.length;
}
function getHashLength2<T extends { __tag__: unknown}>(hash: string & T): number {
if (typeof hash !== "string") {
throw new Error("This doesn't look like a hash");
}
return hash.length;
}
//// [taggedPrimitiveNarrowing.js]
"use strict";
function getHashLength(hash) {
if (typeof hash !== "string") {
throw new Error("This doesn't look like a hash");
}
return hash.length;
}
function getHashLength2(hash) {
if (typeof hash !== "string") {
throw new Error("This doesn't look like a hash");
}
return hash.length;
}
//// [taggedPrimitiveNarrowing.d.ts]
declare type Hash = string & {
__hash: true;
};
declare function getHashLength(hash: Hash): number;
declare function getHashLength2<T extends {
__tag__: unknown;
}>(hash: string & T): number;