file_path stringlengths 3 280 | file_language stringclasses 66 values | content stringlengths 1 1.04M | repo_name stringlengths 5 92 | repo_stars int64 0 154k | repo_description stringlengths 0 402 | repo_primary_language stringclasses 108 values | developer_username stringlengths 1 25 | developer_name stringlengths 0 30 | developer_company stringlengths 0 82 |
|---|---|---|---|---|---|---|---|---|---|
crates/swc_ecma_parser/tests/tsc/typeFromPropertyAssignmentWithExport.ts | TypeScript | // @allowJs: true
// @checkJs: true
// @Filename: a.js
// @outDir: dist
// this is a javascript file...
export const Adapter = {};
Adapter.prop = {};
// comment this out, and it works
Adapter.asyncMethod = function() {} | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeFromPrototypeAssignment.ts | TypeScript | // @noEmit: true
// @allowJs: true
// @checkJs: true
// @Filename: a.js
// @strict: true
// all references to _map, set, get, addon should be ok
/** @constructor */
var Multimap = function() {
this._map = {};
this._map
this.set
this.get
this.addon
};
Multimap.prototype = {
set: function() {
this._map
this.set
this.get
this.addon
},
get() {
this._map
this.set
this.get
this.addon
}
}
Multimap.prototype.addon = function () {
this._map
this.set
this.get
this.addon
}
var mm = new Multimap();
mm._map
mm.set
mm.get
mm.addon
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeFromPrototypeAssignment2.ts | TypeScript | // @noEmit: true
// @allowJs: true
// @checkJs: true
// @Filename: a.js
// @strict: true
// non top-level:
// all references to _map, set, get, addon should be ok
(function container() {
/** @constructor */
var Multimap = function() {
this._map = {};
this._map
this.set
this.get
this.addon
};
Multimap.prototype = {
set: function() {
this._map
this.set
this.get
this.addon
},
get() {
this._map
this.set
this.get
this.addon
}
}
Multimap.prototype.addon = function () {
this._map
this.set
this.get
this.addon
}
var mm = new Multimap();
mm._map
mm.set
mm.get
mm.addon
});
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeFromPrototypeAssignment3.ts | TypeScript | // @noEmit: true
// @allowJs: true
// @checkJs: true
// @Filename: bug26885.js
// @strict: true
function Multimap3() {
this._map = {};
};
Multimap3.prototype = {
/**
* @param {string} key
* @returns {number} the value ok
*/
get(key) {
return this._map[key + ''];
}
}
/** @type {Multimap3} */
const map = new Multimap3();
const n = map.get('hi') | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeFromPrototypeAssignment4.ts | TypeScript | // @allowJs: true
// @checkJs: true
// @emitDeclarationOnly: true
// @declaration: true
// @outDir: out
// @Filename: a.js
function Multimap4() {
this._map = {};
};
Multimap4["prototype"] = {
/**
* @param {string} key
* @returns {number} the value ok
*/
get(key) {
return this._map[key + ''];
}
};
Multimap4["prototype"]["add-on"] = function() {};
Multimap4["prototype"]["addon"] = function() {};
Multimap4["prototype"]["__underscores__"] = function() {};
const map4 = new Multimap4();
map4.get("");
map4["add-on"]();
map4.addon();
map4.__underscores__();
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardEnums.ts | TypeScript | enum E {}
enum V {}
let x: number|string|E|V;
if (typeof x === "number") {
x; // number|E|V
}
else {
x; // string
}
if (typeof x !== "number") {
x; // string
}
else {
x; // number|E|V
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardFunction.ts | TypeScript |
class A {
propA: number;
}
class B {
propB: number;
}
class C extends A {
propC: number;
}
declare function isA(p1: any): p1 is A;
declare function isB(p1: any): p1 is B;
declare function isC(p1: any): p1 is C;
declare function retC(): C;
var a: A;
var b: B;
// Basic
if (isC(a)) {
a.propC;
}
// Sub type
var subType: C;
if(isA(subType)) {
subType.propC;
}
// Union type
var union: A | B;
if(isA(union)) {
union.propA;
}
// Call signature
interface I1 {
(p1: A): p1 is C;
}
// The parameter index and argument index for the type guard target is matching.
// The type predicate type is assignable to the parameter type.
declare function isC_multipleParams(p1, p2): p1 is C;
if (isC_multipleParams(a, 0)) {
a.propC;
}
// Methods
var obj: {
func1(p1: A): p1 is C;
}
class D {
method1(p1: A): p1 is C {
return true;
}
}
// Arrow function
let f1 = (p1: A): p1 is C => false;
// Function type
declare function f2(p1: (p1: A) => p1 is C);
// Function expressions
f2(function(p1: A): p1 is C {
return true;
});
// Evaluations are asssignable to boolean.
declare function acceptingBoolean(a: boolean);
acceptingBoolean(isA(a));
// Type predicates with different parameter name.
declare function acceptingTypeGuardFunction(p1: (item) => item is A);
acceptingTypeGuardFunction(isA);
// Binary expressions
let union2: C | B;
let union3: boolean | B = isA(union2) || union2; | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardFunctionGenerics.ts | TypeScript |
class A {
propA: number;
}
class B {
propB: number;
}
class C extends A {
propC: number;
}
declare function isB(p1): p1 is B;
declare function isC(p1): p1 is C;
declare function retC(x): C;
declare function funA<T>(p1: (p1) => T): T;
declare function funB<T>(p1: (p1) => T, p2: any): p2 is T;
declare function funC<T>(p1: (p1) => p1 is T): T;
declare function funD<T>(p1: (p1) => p1 is T, p2: any): p2 is T;
declare function funE<T, U>(p1: (p1) => p1 is T, p2: U): T;
let a: A;
let test1: boolean = funA(isB);
if (funB(retC, a)) {
a.propC;
}
let test2: B = funC(isB);
if (funD(isC, a)) {
a.propC;
}
let test3: B = funE(isB, 1); | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardFunctionOfFormThis.ts | TypeScript | // @declaration: true
class RoyalGuard {
isLeader(): this is LeadGuard {
return this instanceof LeadGuard;
}
isFollower(): this is FollowerGuard {
return this instanceof FollowerGuard;
}
}
class LeadGuard extends RoyalGuard {
lead(): void {};
}
class FollowerGuard extends RoyalGuard {
follow(): void {};
}
let a: RoyalGuard = new FollowerGuard();
if (a.isLeader()) {
a.lead();
}
else if (a.isFollower()) {
a.follow();
}
interface GuardInterface extends RoyalGuard {}
let b: GuardInterface;
if (b.isLeader()) {
b.lead();
}
else if (b.isFollower()) {
b.follow();
}
// if (((a.isLeader)())) {
// a.lead();
// }
// else if (((a).isFollower())) {
// a.follow();
// }
// if (((a["isLeader"])())) {
// a.lead();
// }
// else if (((a)["isFollower"]())) {
// a.follow();
// }
var holder2 = {a};
if (holder2.a.isLeader()) {
holder2.a;
}
else {
holder2.a;
}
class ArrowGuard {
isElite = (): this is ArrowElite => {
return this instanceof ArrowElite;
}
isMedic = (): this is ArrowMedic => {
return this instanceof ArrowMedic;
}
}
class ArrowElite extends ArrowGuard {
defend(): void {}
}
class ArrowMedic extends ArrowGuard {
heal(): void {}
}
let guard = new ArrowGuard();
if (guard.isElite()) {
guard.defend();
}
else if (guard.isMedic()) {
guard.heal();
}
interface Supplies {
spoiled: boolean;
}
interface Sundries {
broken: boolean;
}
interface Crate<T> {
contents: T;
volume: number;
isSupplies(): this is Crate<Supplies>;
isSundries(): this is Crate<Sundries>;
}
let crate: Crate<{}>;
if (crate.isSundries()) {
crate.contents.broken = true;
}
else if (crate.isSupplies()) {
crate.contents.spoiled = true;
}
// Matching guards should be assignable
a.isFollower = b.isFollower;
a.isLeader = b.isLeader;
class MimicGuard {
isLeader(): this is MimicLeader { return this instanceof MimicLeader; };
isFollower(): this is MimicFollower { return this instanceof MimicFollower; };
}
class MimicLeader extends MimicGuard {
lead(): void {}
}
class MimicFollower extends MimicGuard {
follow(): void {}
}
let mimic = new MimicGuard();
a.isLeader = mimic.isLeader;
a.isFollower = mimic.isFollower;
if (mimic.isFollower()) {
mimic.follow();
mimic.isFollower = a.isFollower;
}
interface MimicGuardInterface {
isLeader(): this is LeadGuard;
isFollower(): this is FollowerGuard;
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardFunctionOfFormThisErrors.ts | TypeScript | // @declaration: true
class RoyalGuard {
isLeader(): this is LeadGuard {
return this instanceof LeadGuard;
}
isFollower(): this is FollowerGuard {
return this instanceof FollowerGuard;
}
}
class LeadGuard extends RoyalGuard {
lead(): void {};
}
class FollowerGuard extends RoyalGuard {
follow(): void {};
}
interface GuardInterface extends RoyalGuard {}
let a: RoyalGuard = new FollowerGuard();
let b: GuardInterface = new LeadGuard();
// Mismatched guards shouldn't be assignable
b.isFollower = b.isLeader;
b.isLeader = b.isFollower;
a.isFollower = a.isLeader;
a.isLeader = a.isFollower;
function invalidGuard(c: any): this is number {
return false;
}
let c: number | number[];
if (invalidGuard(c)) {
c;
}
else {
c;
}
let holder = {invalidGuard};
if (holder.invalidGuard(c)) {
c;
holder;
}
else {
c;
holder;
}
let detached = a.isFollower;
if (detached()) {
a.follow();
}
else {
a.lead();
} | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardInClass.ts | TypeScript | let x: string | number;
if (typeof x === "string") {
let n = class {
constructor() {
let y: string = x;
}
}
}
else {
let m = class {
constructor() {
let y: number = x;
}
}
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardIntersectionTypes.ts | TypeScript | // @strictNullChecks: true
interface X {
x: string;
}
interface Y {
y: string;
}
interface Z {
z: string;
}
declare function isX(obj: any): obj is X;
declare function isY(obj: any): obj is Y;
declare function isZ(obj: any): obj is Z;
function f1(obj: Object) {
if (isX(obj) || isY(obj) || isZ(obj)) {
obj;
}
if (isX(obj) && isY(obj) && isZ(obj)) {
obj;
}
}
// Repro from #8911
// two interfaces
interface A {
a: string;
}
interface B {
b: string;
}
// a type guard for B
function isB(toTest: any): toTest is B {
return toTest && toTest.b;
}
// a function that turns an A into an A & B
function union(a: A): A & B | null {
if (isB(a)) {
return a;
} else {
return null;
}
}
// Repro from #9016
declare function log(s: string): void;
// Supported beast features
interface Beast { wings?: boolean; legs?: number }
interface Legged { legs: number; }
interface Winged { wings: boolean; }
// Beast feature detection via user-defined type guards
function hasLegs(x: Beast): x is Legged { return x && typeof x.legs === 'number'; }
function hasWings(x: Beast): x is Winged { return x && !!x.wings; }
// Function to identify a given beast by detecting its features
function identifyBeast(beast: Beast) {
// All beasts with legs
if (hasLegs(beast)) {
// All winged beasts with legs
if (hasWings(beast)) {
if (beast.legs === 4) {
log(`pegasus - 4 legs, wings`);
}
else if (beast.legs === 2) {
log(`bird - 2 legs, wings`);
}
else {
log(`unknown - ${beast.legs} legs, wings`);
}
}
// All non-winged beasts with legs
else {
log(`manbearpig - ${beast.legs} legs, no wings`);
}
}
// All beasts without legs
else {
if (hasWings(beast)) {
log(`quetzalcoatl - no legs, wings`)
}
else {
log(`snake - no legs, no wings`)
}
}
}
function beastFoo(beast: Object) {
if (hasWings(beast) && hasLegs(beast)) {
beast; // Winged & Legged
}
else {
beast;
}
if (hasLegs(beast) && hasWings(beast)) {
beast; // Legged & Winged
}
} | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardNarrowsPrimitiveIntersection.ts | TypeScript | type Tag = {__tag: any};
declare function isNonBlank(value: string) : value is (string & Tag);
declare function doThis(value: string & Tag): void;
declare function doThat(value: string) : void;
let value: string;
if (isNonBlank(value)) {
doThis(value);
} else {
doThat(value);
}
const enum Tag2 {}
declare function isNonBlank2(value: string) : value is (string & Tag2);
declare function doThis2(value: string & Tag2): void;
declare function doThat2(value: string) : void;
if (isNonBlank2(value)) {
doThis2(value);
} else {
doThat2(value);
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardNarrowsToLiteralType.ts | TypeScript | declare function isFoo(value: string) : value is "foo";
declare function doThis(value: "foo"): void;
declare function doThat(value: string) : void;
let value: string;
if (isFoo(value)) {
doThis(value);
} else {
doThat(value);
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardNarrowsToLiteralTypeUnion.ts | TypeScript | declare function isFoo(value: string) : value is ("foo" | "bar");
declare function doThis(value: "foo" | "bar"): void;
declare function doThat(value: string) : void;
let value: string;
if (isFoo(value)) {
doThis(value);
} else {
doThat(value);
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardNesting.ts | TypeScript | let strOrBool: string|boolean;
if ((typeof strOrBool === 'boolean' && !strOrBool) || typeof strOrBool === 'string') {
let label: string = (typeof strOrBool === 'string') ? strOrBool : "string";
let bool: boolean = (typeof strOrBool === 'boolean') ? strOrBool : false;
let label2: string = (typeof strOrBool !== 'boolean') ? strOrBool : "string";
let bool2: boolean = (typeof strOrBool !== 'string') ? strOrBool : false;
}
if ((typeof strOrBool !== 'string' && !strOrBool) || typeof strOrBool !== 'boolean') {
let label: string = (typeof strOrBool === 'string') ? strOrBool : "string";
let bool: boolean = (typeof strOrBool === 'boolean') ? strOrBool : false;
let label2: string = (typeof strOrBool !== 'boolean') ? strOrBool : "string";
let bool2: boolean = (typeof strOrBool !== 'string') ? strOrBool : false;
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardOfFormExpr1AndExpr2.ts | TypeScript | var str: string;
var bool: boolean;
var num: number;
var strOrNum: string | number;
var strOrNumOrBool: string | number | boolean;
var numOrBool: number | boolean;
class C { private p; }
var c: C;
var cOrBool: C| boolean;
var strOrNumOrBoolOrC: string | number | boolean | C;
// A type guard of the form expr1 && expr2
// - when true, narrows the type of x by expr1 when true and then by expr2 when true, or
// - when false, narrows the type of x to T1 | T2, where T1 is the type of x narrowed by expr1 when
// false, and T2 is the type of x narrowed by expr1 when true and then by expr2 when false.
// (typeguard1 && typeguard2)
if (typeof strOrNumOrBool !== "string" && typeof strOrNumOrBool !== "number") {
bool = strOrNumOrBool; // boolean
}
else {
strOrNum = strOrNumOrBool; // string | number
}
// (typeguard1 && typeguard2 && typeguard3)
if (typeof strOrNumOrBoolOrC !== "string" && typeof strOrNumOrBoolOrC !== "number" && typeof strOrNumOrBoolOrC !== "boolean") {
c = strOrNumOrBoolOrC; // C
}
else {
strOrNumOrBool = strOrNumOrBoolOrC; // string | number | boolean
}
// (typeguard1 && typeguard2 && typeguard11(onAnotherType))
if (typeof strOrNumOrBoolOrC !== "string" && typeof strOrNumOrBoolOrC !== "number" && typeof strOrNumOrBool === "boolean") {
cOrBool = strOrNumOrBoolOrC; // C | boolean
bool = strOrNumOrBool; // boolean
}
else {
var r1: string | number | boolean | C = strOrNumOrBoolOrC; // string | number | boolean | C
var r2: string | number | boolean = strOrNumOrBool;
}
// (typeguard1) && simpleExpr
if (typeof strOrNumOrBool !== "string" && numOrBool !== strOrNumOrBool) {
numOrBool = strOrNumOrBool; // number | boolean
}
else {
var r3: string | number | boolean = strOrNumOrBool; // string | number | boolean
} | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardOfFormExpr1OrExpr2.ts | TypeScript | var str: string;
var bool: boolean;
var num: number;
var strOrNum: string | number;
var strOrNumOrBool: string | number | boolean;
var numOrBool: number | boolean;
class C { private p; }
var c: C;
var cOrBool: C| boolean;
var strOrNumOrBoolOrC: string | number | boolean | C;
// A type guard of the form expr1 || expr2
// - when true, narrows the type of x to T1 | T2, where T1 is the type of x narrowed by expr1 when true,
// and T2 is the type of x narrowed by expr1 when false and then by expr2 when true, or
// - when false, narrows the type of x by expr1 when false and then by expr2 when false.
// (typeguard1 || typeguard2)
if (typeof strOrNumOrBool === "string" || typeof strOrNumOrBool === "number") {
strOrNum = strOrNumOrBool; // string | number
}
else {
bool = strOrNumOrBool; // boolean
}
// (typeguard1 || typeguard2 || typeguard3)
if (typeof strOrNumOrBoolOrC === "string" || typeof strOrNumOrBoolOrC === "number" || typeof strOrNumOrBoolOrC === "boolean") {
strOrNumOrBool = strOrNumOrBoolOrC; // string | number | boolean
}
else {
c = strOrNumOrBoolOrC; // C
}
// (typeguard1 || typeguard2 || typeguard11(onAnotherType))
if (typeof strOrNumOrBoolOrC === "string" || typeof strOrNumOrBoolOrC === "number" || typeof strOrNumOrBool !== "boolean") {
var r1: string | number | boolean | C = strOrNumOrBoolOrC; // string | number | boolean | C
var r2: string | number | boolean = strOrNumOrBool;
}
else {
cOrBool = strOrNumOrBoolOrC; // C | boolean
bool = strOrNumOrBool; // boolean
}
// (typeguard1) || simpleExpr
if (typeof strOrNumOrBool === "string" || numOrBool !== strOrNumOrBool) {
var r3: string | number | boolean = strOrNumOrBool; // string | number | boolean
}
else {
numOrBool = strOrNumOrBool; // number | boolean
} | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardOfFormFunctionEquality.ts | TypeScript | declare function isString1(a: number, b: Object): b is string;
declare function isString2(a: Object): a is string;
switch (isString1(0, "")) {
case isString2(""):
default:
}
var x = isString1(0, "") === isString2("");
function isString3(a: number, b: number, c: Object): c is string {
return isString1(0, c);
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardOfFormInstanceOf.ts | TypeScript | // A type guard of the form x instanceof C, where C is of a subtype of the global type 'Function'
// and C has a property named 'prototype'
// - when true, narrows the type of x to the type of the 'prototype' property in C provided
// it is a subtype of the type of x, or
// - when false, has no effect on the type of x.
class C1 {
p1: string;
}
class C2 {
p2: number;
}
class D1 extends C1 {
p3: number;
}
class C3 {
p4: number;
}
var str: string;
var num: number;
var strOrNum: string | number;
var ctor1: C1 | C2;
str = ctor1 instanceof C1 && ctor1.p1; // C1
num = ctor1 instanceof C2 && ctor1.p2; // C2
str = ctor1 instanceof D1 && ctor1.p1; // D1
num = ctor1 instanceof D1 && ctor1.p3; // D1
var ctor2: C2 | D1;
num = ctor2 instanceof C2 && ctor2.p2; // C2
num = ctor2 instanceof D1 && ctor2.p3; // D1
str = ctor2 instanceof D1 && ctor2.p1; // D1
var r2: D1 | C2 = ctor2 instanceof C1 && ctor2; // C2 | D1
var ctor3: C1 | C2;
if (ctor3 instanceof C1) {
ctor3.p1; // C1
}
else {
ctor3.p2; // C2
}
var ctor4: C1 | C2 | C3;
if (ctor4 instanceof C1) {
ctor4.p1; // C1
}
else if (ctor4 instanceof C2) {
ctor4.p2; // C2
}
else {
ctor4.p4; // C3
}
var ctor5: C1 | D1 | C2;
if (ctor5 instanceof C1) {
ctor5.p1; // C1
}
else {
ctor5.p2; // C2
}
var ctor6: C1 | C2 | C3;
if (ctor6 instanceof C1 || ctor6 instanceof C2) {
}
else {
ctor6.p4; // C3
} | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardOfFormInstanceOfOnInterface.ts | TypeScript | // A type guard of the form x instanceof C, where C is of a subtype of the global type 'Function'
// and C has a property named 'prototype'
// - when true, narrows the type of x to the type of the 'prototype' property in C provided
// it is a subtype of the type of x, or
// - when false, has no effect on the type of x.
interface C1 {
(): C1;
prototype: C1;
p1: string;
}
interface C2 {
(): C2;
prototype: C2;
p2: number;
}
interface D1 extends C1 {
prototype: D1;
p3: number;
}
var str: string;
var num: number;
var strOrNum: string | number;
var c1: C1;
var c2: C2;
var d1: D1;
var c1Orc2: C1 | C2;
str = c1Orc2 instanceof c1 && c1Orc2.p1; // C1
num = c1Orc2 instanceof c2 && c1Orc2.p2; // C2
str = c1Orc2 instanceof d1 && c1Orc2.p1; // C1
num = c1Orc2 instanceof d1 && c1Orc2.p3; // D1
var c2Ord1: C2 | D1;
num = c2Ord1 instanceof c2 && c2Ord1.p2; // C2
num = c2Ord1 instanceof d1 && c2Ord1.p3; // D1
str = c2Ord1 instanceof d1 && c2Ord1.p1; // D1
var r2: D1 | C2 = c2Ord1 instanceof c1 && c2Ord1; // C2 | D1 | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardOfFormIsType.ts | TypeScript |
class C1 {
p1: string;
}
class C2 {
p2: number;
}
class D1 extends C1 {
p3: number;
}
var str: string;
var num: number;
var strOrNum: string | number;
function isC1(x: any): x is C1 {
return true;
}
function isC2(x: any): x is C2 {
return true;
}
function isD1(x: any): x is D1 {
return true;
}
var c1Orc2: C1 | C2;
str = isC1(c1Orc2) && c1Orc2.p1; // C1
num = isC2(c1Orc2) && c1Orc2.p2; // C2
str = isD1(c1Orc2) && c1Orc2.p1; // D1
num = isD1(c1Orc2) && c1Orc2.p3; // D1
var c2Ord1: C2 | D1;
num = isC2(c2Ord1) && c2Ord1.p2; // C2
num = isD1(c2Ord1) && c2Ord1.p3; // D1
str = isD1(c2Ord1) && c2Ord1.p1; // D1
var r2: C2 | D1 = isC1(c2Ord1) && c2Ord1; // C2 | D1 | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardOfFormIsTypeOnInterfaces.ts | TypeScript |
interface C1 {
(): C1;
prototype: C1;
p1: string;
}
interface C2 {
(): C2;
prototype: C2;
p2: number;
}
interface D1 extends C1 {
prototype: D1;
p3: number;
}
var str: string;
var num: number;
var strOrNum: string | number;
function isC1(x: any): x is C1 {
return true;
}
function isC2(x: any): x is C2 {
return true;
}
function isD1(x: any): x is D1 {
return true;
}
var c1: C1;
var c2: C2;
var d1: D1;
var c1Orc2: C1 | C2;
str = isC1(c1Orc2) && c1Orc2.p1; // C1
num = isC2(c1Orc2) && c1Orc2.p2; // C2
str = isD1(c1Orc2) && c1Orc2.p1; // D1
num = isD1(c1Orc2) && c1Orc2.p3; // D1
var c2Ord1: C2 | D1;
num = isC2(c2Ord1) && c2Ord1.p2; // C2
num = isD1(c2Ord1) && c2Ord1.p3; // D1
str = isD1(c2Ord1) && c2Ord1.p1; // D1
var r2: C2 | D1 = isC1(c2Ord1) && c2Ord1; // C2 | D1 | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardOfFormNotExpr.ts | TypeScript | var str: string;
var bool: boolean;
var num: number;
var strOrNum: string | number;
var strOrNumOrBool: string | number | boolean;
var numOrBool: number | boolean;
// A type guard of the form !expr
// - when true, narrows the type of x by expr when false, or
// - when false, narrows the type of x by expr when true.
// !typeguard1
if (!(typeof strOrNum === "string")) {
num === strOrNum; // number
}
else {
str = strOrNum; // string
}
// !(typeguard1 || typeguard2)
if (!(typeof strOrNumOrBool === "string" || typeof strOrNumOrBool === "number")) {
bool = strOrNumOrBool; // boolean
}
else {
strOrNum = strOrNumOrBool; // string | number
}
// !(typeguard1) || !(typeguard2)
if (!(typeof strOrNumOrBool !== "string") || !(typeof strOrNumOrBool !== "number")) {
strOrNum = strOrNumOrBool; // string | number
}
else {
bool = strOrNumOrBool; // boolean
}
// !(typeguard1 && typeguard2)
if (!(typeof strOrNumOrBool !== "string" && typeof strOrNumOrBool !== "number")) {
strOrNum = strOrNumOrBool; // string | number
}
else {
bool = strOrNumOrBool; // boolean
}
// !(typeguard1) && !(typeguard2)
if (!(typeof strOrNumOrBool === "string") && !(typeof strOrNumOrBool === "number")) {
bool = strOrNumOrBool; // boolean
}
else {
strOrNum = strOrNumOrBool; // string | number
}
// !(typeguard1) && simpleExpr
if (!(typeof strOrNumOrBool === "string") && numOrBool !== strOrNumOrBool) {
numOrBool = strOrNumOrBool; // number | boolean
}
else {
var r1: string | number | boolean = strOrNumOrBool; // string | number | boolean
} | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardOfFormTypeOfBoolean.ts | TypeScript | class C { private p: string };
var str: string;
var bool: boolean;
var num: number;
var strOrNum: string | number;
var strOrBool: string | boolean;
var numOrBool: number | boolean
var strOrNumOrBool: string | number | boolean;
var strOrC: string | C;
var numOrC: number | C;
var boolOrC: boolean | C;
var c: C;
// A type guard of the form typeof x === s,
// where s is a string literal with the value 'string', 'number', or 'boolean',
// - when true, narrows the type of x to the given primitive type, or
// - when false, removes the primitive type from the type of x.
if (typeof strOrBool === "boolean") {
bool = strOrBool; // boolean
}
else {
str = strOrBool; // string
}
if (typeof numOrBool === "boolean") {
bool = numOrBool; // boolean
}
else {
num = numOrBool; // number
}
if (typeof strOrNumOrBool === "boolean") {
bool = strOrNumOrBool; // boolean
}
else {
strOrNum = strOrNumOrBool; // string | number
}
if (typeof boolOrC === "boolean") {
bool = boolOrC; // boolean
}
else {
c = boolOrC; // C
}
if (typeof strOrNum === "boolean") {
let z1: {} = strOrNum; // {}
}
else {
let z2: string | number = strOrNum; // string | number
}
// A type guard of the form typeof x !== s, where s is a string literal,
// - when true, narrows the type of x by typeof x === s when false, or
// - when false, narrows the type of x by typeof x === s when true.
if (typeof strOrBool !== "boolean") {
str = strOrBool; // string
}
else {
bool = strOrBool; // boolean
}
if (typeof numOrBool !== "boolean") {
num = numOrBool; // number
}
else {
bool = numOrBool; // boolean
}
if (typeof strOrNumOrBool !== "boolean") {
strOrNum = strOrNumOrBool; // string | number
}
else {
bool = strOrNumOrBool; // boolean
}
if (typeof boolOrC !== "boolean") {
c = boolOrC; // C
}
else {
bool = boolOrC; // boolean
}
if (typeof strOrNum !== "boolean") {
let z1: string | number = strOrNum; // string | number
}
else {
let z2: {} = strOrNum; // {}
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardOfFormTypeOfEqualEqualHasNoEffect.ts | TypeScript | class C { private p: string };
var strOrNum: string | number;
var strOrBool: string | boolean;
var numOrBool: number | boolean
var strOrC: string | C;
// typeof x == s has not effect on typeguard
if (typeof strOrNum == "string") {
var r1 = strOrNum; // string | number
}
else {
var r1 = strOrNum; // string | number
}
if (typeof strOrBool == "boolean") {
var r2 = strOrBool; // string | boolean
}
else {
var r2 = strOrBool; // string | boolean
}
if (typeof numOrBool == "number") {
var r3 = numOrBool; // number | boolean
}
else {
var r3 = numOrBool; // number | boolean
}
if (typeof strOrC == "Object") {
var r4 = strOrC; // string | C
}
else {
var r4 = strOrC; // string | C
} | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardOfFormTypeOfFunction.ts | TypeScript |
function f1(x: any) {
if (typeof x === "function") {
x; // any
}
}
function f2(x: unknown) {
if (typeof x === "function") {
x; // Function
}
}
function f3(x: {}) {
if (typeof x === "function") {
x; // Function
}
}
function f4<T>(x: T) {
if (typeof x === "function") {
x; // T & Function
}
}
function f5(x: { s: string }) {
if (typeof x === "function") {
x; // never
}
}
function f6(x: () => string) {
if (typeof x === "function") {
x; // () => string
}
}
function f10(x: string | (() => string)) {
if (typeof x === "function") {
x; // () => string
}
else {
x; // string
}
}
function f11(x: { s: string } | (() => string)) {
if (typeof x === "function") {
x; // () => string
}
else {
x; // { s: string }
}
}
function f12(x: { s: string } | { n: number }) {
if (typeof x === "function") {
x; // never
}
else {
x; // { s: string } | { n: number }
}
}
// Repro from #18238
function f100<T, K extends keyof T>(obj: T, keys: K[]) : void {
for (const k of keys) {
const item = obj[k];
if (typeof item == 'function')
item.call(obj);
}
}
// Repro from #49316
function configureStore<S extends object>(reducer: (() => void) | Record<keyof S, () => void>) {
let rootReducer: () => void;
if (typeof reducer === 'function') {
rootReducer = reducer;
}
}
function f101(x: string | Record<string, any>) {
return typeof x === "object" && x.anything;
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardOfFormTypeOfIsOrderIndependent.ts | TypeScript | var strOrNum: string | number;
var strOrBool: string | boolean;
var strOrFunc: string | (() => void);
var numOrBool: number | boolean
var str: string;
var num: number;
var bool: boolean;
var func: () => void;
if ("string" === typeof strOrNum) {
str = strOrNum;
}
else {
num = strOrNum;
}
if ("function" === typeof strOrFunc) {
func = strOrFunc;
}
else {
str = strOrFunc;
}
if ("number" === typeof numOrBool) {
num = numOrBool;
}
else {
bool = numOrBool;
}
if ("boolean" === typeof strOrBool) {
bool = strOrBool;
}
else {
str = strOrBool;
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardOfFormTypeOfNotEqualHasNoEffect.ts | TypeScript | class C { private p: string };
var strOrNum: string | number;
var strOrBool: string | boolean;
var numOrBool: number | boolean
var strOrC: string | C;
// typeof x != s has not effect on typeguard
if (typeof strOrNum != "string") {
var r1 = strOrNum; // string | number
}
else {
var r1 = strOrNum; // string | number
}
if (typeof strOrBool != "boolean") {
var r2 = strOrBool; // string | boolean
}
else {
var r2 = strOrBool; // string | boolean
}
if (typeof numOrBool != "number") {
var r3 = numOrBool; // number | boolean
}
else {
var r3 = numOrBool; // number | boolean
}
if (typeof strOrC != "Object") {
var r4 = strOrC; // string | C
}
else {
var r4 = strOrC; // string | C
} | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardOfFormTypeOfNumber.ts | TypeScript | class C { private p: string };
var str: string;
var bool: boolean;
var num: number;
var strOrNum: string | number;
var strOrBool: string | boolean;
var numOrBool: number | boolean
var strOrNumOrBool: string | number | boolean;
var strOrC: string | C;
var numOrC: number | C;
var boolOrC: boolean | C;
var c: C;
// A type guard of the form typeof x === s,
// where s is a string literal with the value 'string', 'number', or 'boolean',
// - when true, narrows the type of x to the given primitive type, or
// - when false, removes the primitive type from the type of x.
if (typeof strOrNum === "number") {
num = strOrNum; // number
}
else {
str === strOrNum; // string
}
if (typeof numOrBool === "number") {
num = numOrBool; // number
}
else {
var x: number | boolean = numOrBool; // number | boolean
}
if (typeof strOrNumOrBool === "number") {
num = strOrNumOrBool; // number
}
else {
strOrBool = strOrNumOrBool; // string | boolean
}
if (typeof numOrC === "number") {
num = numOrC; // number
}
else {
c = numOrC; // C
}
if (typeof strOrBool === "number") {
let y1: {} = strOrBool; // {}
}
else {
let y2: string | boolean = strOrBool; // string | boolean
}
// A type guard of the form typeof x !== s, where s is a string literal,
// - when true, narrows the type of x by typeof x === s when false, or
// - when false, narrows the type of x by typeof x === s when true.
if (typeof strOrNum !== "number") {
str === strOrNum; // string
}
else {
num = strOrNum; // number
}
if (typeof numOrBool !== "number") {
var x: number | boolean = numOrBool; // number | boolean
}
else {
num = numOrBool; // number
}
if (typeof strOrNumOrBool !== "number") {
strOrBool = strOrNumOrBool; // string | boolean
}
else {
num = strOrNumOrBool; // number
}
if (typeof numOrC !== "number") {
c = numOrC; // C
}
else {
num = numOrC; // number
}
if (typeof strOrBool !== "number") {
let y1: string | boolean = strOrBool; // string | boolean
}
else {
let y2: {} = strOrBool; // {}
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardOfFormTypeOfOther.ts | TypeScript | class C { private p: string };
var str: string;
var bool: boolean;
var num: number;
var strOrNum: string | number;
var strOrBool: string | boolean;
var numOrBool: number | boolean
var strOrNumOrBool: string | number | boolean;
var strOrC: string | C;
var numOrC: number | C;
var boolOrC: boolean | C;
var emptyObj: {};
var c: C;
// A type guard of the form typeof x === s,
// where s is a string literal with any value but 'string', 'number' or 'boolean',
// - when true, removes the primitive types string, number, and boolean from the type of x, or
// - when false, has no effect on the type of x.
if (typeof strOrC === "Object") {
c = strOrC; // C
}
else {
var r2: string = strOrC; // string
}
if (typeof numOrC === "Object") {
c = numOrC; // C
}
else {
var r3: number = numOrC; // number
}
if (typeof boolOrC === "Object") {
c = boolOrC; // C
}
else {
var r4: boolean = boolOrC; // boolean
}
if (typeof strOrC === "Object" as string) { // comparison is OK with cast
c = strOrC; // error: but no narrowing to C
}
else {
var r5: string = strOrC; // error: no narrowing to string
}
if (typeof strOrNumOrBool === "Object") {
let q1: {} = strOrNumOrBool; // {}
}
else {
let q2: string | number | boolean = strOrNumOrBool; // string | number | boolean
}
// A type guard of the form typeof x !== s, where s is a string literal,
// - when true, narrows the type of x by typeof x === s when false, or
// - when false, narrows the type of x by typeof x === s when true.
if (typeof strOrC !== "Object") {
var r2: string = strOrC; // string
}
else {
c = strOrC; // C
}
if (typeof numOrC !== "Object") {
var r3: number = numOrC; // number
}
else {
c = numOrC; // C
}
if (typeof boolOrC !== "Object") {
var r4: boolean = boolOrC; // boolean
}
else {
c = boolOrC; // C
}
if (typeof strOrNumOrBool !== "Object") {
let q1: string | number | boolean = strOrNumOrBool; // string | number | boolean
}
else {
let q2: {} = strOrNumOrBool; // {}
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardOfFormTypeOfPrimitiveSubtype.ts | TypeScript | let a: {};
let b: {toString(): string};
if (typeof a === "number") {
let c: number = a;
}
if (typeof a === "string") {
let c: string = a;
}
if (typeof a === "boolean") {
let c: boolean = a;
}
if (typeof b === "number") {
let c: number = b;
}
if (typeof b === "string") {
let c: string = b;
}
if (typeof b === "boolean") {
let c: boolean = b;
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardOfFormTypeOfString.ts | TypeScript | class C { private p: string };
var str: string;
var bool: boolean;
var num: number;
var strOrNum: string | number;
var strOrBool: string | boolean;
var numOrBool: number | boolean
var strOrNumOrBool: string | number | boolean;
var strOrC: string | C;
var numOrC: number | C;
var boolOrC: boolean | C;
var c: C;
// A type guard of the form typeof x === s,
// where s is a string literal with the value 'string', 'number', or 'boolean',
// - when true, narrows the type of x to the given primitive type, or
// - when false, removes the primitive type from the type of x.
if (typeof strOrNum === "string") {
str = strOrNum; // string
}
else {
num === strOrNum; // number
}
if (typeof strOrBool === "string") {
str = strOrBool; // string
}
else {
bool = strOrBool; // boolean
}
if (typeof strOrNumOrBool === "string") {
str = strOrNumOrBool; // string
}
else {
numOrBool = strOrNumOrBool; // number | boolean
}
if (typeof strOrC === "string") {
str = strOrC; // string
}
else {
c = strOrC; // C
}
if (typeof numOrBool === "string") {
let x1: {} = numOrBool; // {}
}
else {
let x2: number | boolean = numOrBool; // number | boolean
}
// A type guard of the form typeof x !== s, where s is a string literal,
// - when true, narrows the type of x by typeof x === s when false, or
// - when false, narrows the type of x by typeof x === s when true.
if (typeof strOrNum !== "string") {
num === strOrNum; // number
}
else {
str = strOrNum; // string
}
if (typeof strOrBool !== "string") {
bool = strOrBool; // boolean
}
else {
str = strOrBool; // string
}
if (typeof strOrNumOrBool !== "string") {
numOrBool = strOrNumOrBool; // number | boolean
}
else {
str = strOrNumOrBool; // string
}
if (typeof strOrC !== "string") {
c = strOrC; // C
}
else {
str = strOrC; // string
}
if (typeof numOrBool !== "string") {
let x1: number | boolean = numOrBool; // number | boolean
}
else {
let x2: {} = numOrBool; // {}
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardOfFromPropNameInUnionType.ts | TypeScript | class A { a: string; }
class B { b: number; }
class C { b: Object; }
class D { a: Date; }
function namedClasses(x: A | B) {
if ("a" in x) {
x.a = "1";
} else {
x.b = 1;
}
}
function multipleClasses(x: A | B | C | D) {
if ("a" in x) {
let y: string | Date = x.a;
} else {
let z: number | Object = x.b;
}
}
function anonymousClasses(x: { a: string; } | { b: number; }) {
if ("a" in x) {
let y: string = x.a;
} else {
let z: number = x.b;
}
}
class AWithOptionalProp { a?: string; }
class BWithOptionalProp { b?: string; }
function positiveTestClassesWithOptionalProperties(x: AWithOptionalProp | BWithOptionalProp) {
if ("a" in x) {
x.a = "1";
} else {
const y: string = x instanceof AWithOptionalProp
? x.a
: x.b
}
}
function inParenthesizedExpression(x: A | B) {
if ("a" in (x)) {
let y: string = x.a;
} else {
let z: number = x.b;
}
}
class ClassWithUnionProp { prop: A | B; }
function inProperty(x: ClassWithUnionProp) {
if ("a" in x.prop) {
let y: string = x.prop.a;
} else {
let z: number = x.prop.b;
}
}
class NestedClassWithProp { outer: ClassWithUnionProp; }
function innestedProperty(x: NestedClassWithProp) {
if ("a" in x.outer.prop) {
let y: string = x.outer.prop.a;
} else {
let z: number = x.outer.prop.b;
}
}
class InMemberOfClass {
protected prop: A | B;
inThis() {
if ("a" in this.prop) {
let y: string = this.prop.a;
} else {
let z: number = this.prop.b;
}
}
}
// added for completeness
class SelfAssert {
a: string;
inThis() {
if ("a" in this) {
let y: string = this.a;
} else {
}
}
}
interface Indexed {
[s: string]: any;
}
function f(i: Indexed) {
if ("a" in i) {
return i.a;
}
else if ("b" in i) {
return i.b;
}
return "c" in i && i.c;
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardRedundancy.ts | TypeScript | var x: string|number;
var r1 = typeof x === "string" && typeof x === "string" ? x.substr : x.toFixed;
var r2 = !(typeof x === "string" && typeof x === "string") ? x.toFixed : x.substr;
var r3 = typeof x === "string" || typeof x === "string" ? x.substr : x.toFixed;
var r4 = !(typeof x === "string" || typeof x === "string") ? x.toFixed : x.substr; | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardTautologicalConsistiency.ts | TypeScript | let stringOrNumber: string | number;
if (typeof stringOrNumber === "number") {
if (typeof stringOrNumber !== "number") {
stringOrNumber;
}
}
if (typeof stringOrNumber === "number" && typeof stringOrNumber !== "number") {
stringOrNumber;
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardTypeOfUndefined.ts | TypeScript | // undefined type guard adds no new type information
function test1(a: any) {
if (typeof a !== "undefined") {
if (typeof a === "boolean") {
a;
}
else {
a;
}
}
else {
a;
}
}
function test2(a: any) {
if (typeof a === "undefined") {
if (typeof a === "boolean") {
a;
}
else {
a;
}
}
else {
a;
}
}
function test3(a: any) {
if (typeof a === "undefined" || typeof a === "boolean") {
a;
}
else {
a;
}
}
function test4(a: any) {
if (typeof a !== "undefined" && typeof a === "boolean") {
a;
}
else {
a;
}
}
function test5(a: boolean | void) {
if (typeof a !== "undefined") {
if (typeof a === "boolean") {
a;
}
else {
a;
}
}
else {
a;
}
}
function test6(a: boolean | void) {
if (typeof a === "undefined") {
if (typeof a === "boolean") {
a;
}
else {
a;
}
}
else {
a;
}
}
function test7(a: boolean | void) {
if (typeof a === "undefined" || typeof a === "boolean") {
a;
}
else {
a;
}
}
function test8(a: boolean | void) {
if (typeof a !== "undefined" && typeof a === "boolean") {
a;
}
else {
a;
}
}
function test9(a: boolean | number) {
if (typeof a !== "undefined") {
if (typeof a === "boolean") {
a;
}
else {
a;
}
}
else {
a;
}
}
function test10(a: boolean | number) {
if (typeof a === "undefined") {
if (typeof a === "boolean") {
a;
}
else {
a;
}
}
else {
a;
}
}
function test11(a: boolean | number) {
if (typeof a === "undefined" || typeof a === "boolean") {
a;
}
else {
a;
}
}
function test12(a: boolean | number) {
if (typeof a !== "undefined" && typeof a === "boolean") {
a;
}
else {
a;
}
}
function test13(a: boolean | number | void) {
if (typeof a !== "undefined") {
if (typeof a === "boolean") {
a;
}
else {
a;
}
}
else {
a;
}
}
function test14(a: boolean | number | void) {
if (typeof a === "undefined") {
if (typeof a === "boolean") {
a;
}
else {
a;
}
}
else {
a;
}
}
function test15(a: boolean | number | void) {
if (typeof a === "undefined" || typeof a === "boolean") {
a;
}
else {
a;
}
}
function test16(a: boolean | number | void) {
if (typeof a !== "undefined" && typeof a === "boolean") {
a;
}
else {
a;
}
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardsAsAssertions.ts | TypeScript | // @strictNullChecks: true
// Repro from #8513
let cond: boolean;
export type Optional<a> = Some<a> | None;
export interface None { readonly none: string; }
export interface Some<a> { readonly some: a; }
export const none : None = { none: '' };
export function isSome<a>(value: Optional<a>): value is Some<a> {
return 'some' in value;
}
function someFrom<a>(some: a) {
return { some };
}
export function fn<r>(makeSome: () => r): void {
let result: Optional<r> = none;
result; // None
while (cond) {
result; // Some<r> | None
result = someFrom(isSome(result) ? result.some : makeSome());
result; // Some<r>
}
}
function foo1() {
let x: string | number | boolean = 0;
x; // number
while (cond) {
x; // number, then string | number
x = typeof x === "string" ? x.slice() : "abc";
x; // string
}
x;
}
function foo2() {
let x: string | number | boolean = 0;
x; // number
while (cond) {
x; // number, then string | number
if (typeof x === "string") {
x = x.slice();
}
else {
x = "abc";
}
x; // string
}
x;
}
// Type guards as assertions
function f1() {
let x: string | number | undefined = undefined;
x; // undefined
if (x) {
x; // string | number (guard as assertion)
}
x; // string | number | undefined
}
function f2() {
let x: string | number | undefined = undefined;
x; // undefined
if (typeof x === "string") {
x; // string (guard as assertion)
}
x; // string | undefined
}
function f3() {
let x: string | number | undefined = undefined;
x; // undefined
if (!x) {
return;
}
x; // string | number (guard as assertion)
}
function f4() {
let x: string | number | undefined = undefined;
x; // undefined
if (typeof x === "boolean") {
x; // nothing (boolean not in declared type)
}
x; // undefined
}
function f5(x: string | number) {
if (typeof x === "string" && typeof x === "number") {
x; // number (guard as assertion)
}
else {
x; // string | number
}
x; // string | number
}
function f6() {
let x: string | undefined | null;
x!.slice();
x = "";
x!.slice();
x = undefined;
x!.slice();
x = null;
x!.slice();
x = <undefined | null>undefined;
x!.slice();
x = <string | undefined>"";
x!.slice();
x = <string | null>"";
x!.slice();
}
function f7() {
let x: string;
x!.slice();
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardsDefeat.ts | TypeScript | // Also note that it is possible to defeat a type guard by calling a function that changes the
// type of the guarded variable.
function foo(x: number | string) {
function f() {
x = 10;
}
if (typeof x === "string") {
f();
return x.length; // string
}
else {
return x++; // number
}
}
function foo2(x: number | string) {
if (typeof x === "string") {
return x.length; // string
}
else {
var f = function () {
return x * x;
};
}
x = "hello";
f();
}
function foo3(x: number | string) {
if (typeof x === "string") {
return x.length; // string
}
else {
var f = () => x * x;
}
x = "hello";
f();
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardsInClassAccessors.ts | TypeScript | //@target: es5
// Note that type guards affect types of variables and parameters only and
// have no effect on members of objects such as properties.
// variables in global
var num: number;
var strOrNum: string | number;
var var1: string | number;
class ClassWithAccessors {
// Inside public accessor getter
get p1() {
// global vars in function declaration
num = typeof var1 === "string" && var1.length; // string
// variables in function declaration
var var2: string | number;
num = typeof var2 === "string" && var2.length; // string
return strOrNum;
}
// Inside public accessor setter
set p1(param: string | number) {
// global vars in function declaration
num = typeof var1 === "string" && var1.length; // string
// parameter of function declaration
num = typeof param === "string" && param.length; // string
// variables in function declaration
var var2: string | number;
num = typeof var2 === "string" && var2.length; // string
}
// Inside private accessor getter
private get pp1() {
// global vars in function declaration
num = typeof var1 === "string" && var1.length; // string
// variables in function declaration
var var2: string | number;
num = typeof var2 === "string" && var2.length; // string
return strOrNum;
}
// Inside private accessor setter
private set pp1(param: string | number) {
// global vars in function declaration
num = typeof var1 === "string" && var1.length; // string
// parameter of function declaration
num = typeof param === "string" && param.length; // string
// variables in function declaration
var var2: string | number;
num = typeof var2 === "string" && var2.length; // string
}
// Inside static accessor getter
static get s1() {
// global vars in function declaration
num = typeof var1 === "string" && var1.length; // string
// variables in function declaration
var var2: string | number;
num = typeof var2 === "string" && var2.length; // string
return strOrNum;
}
// Inside static accessor setter
static set s1(param: string | number) {
// global vars in function declaration
num = typeof var1 === "string" && var1.length; // string
// parameter of function declaration
num = typeof param === "string" && param.length; // string
// variables in function declaration
var var2: string | number;
num = typeof var2 === "string" && var2.length; // string
}
// Inside private static accessor getter
private static get ss1() {
// global vars in function declaration
num = typeof var1 === "string" && var1.length; // string
// variables in function declaration
var var2: string | number;
num = typeof var2 === "string" && var2.length; // string
return strOrNum;
}
// Inside private static accessor setter
private static set ss1(param: string | number) {
// global vars in function declaration
num = typeof var1 === "string" && var1.length; // string
// parameter of function declaration
num = typeof param === "string" && param.length; // string
// variables in function declaration
var var2: string | number;
num = typeof var2 === "string" && var2.length; // string
}
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardsInClassMethods.ts | TypeScript | // Note that type guards affect types of variables and parameters only and
// have no effect on members of objects such as properties.
// variables in global
var num: number;
var var1: string | number;
class C1 {
constructor(param: string | number) {
// global vars in function declaration
num = typeof var1 === "string" && var1.length; // string
// variables in function declaration
var var2: string | number;
num = typeof var2 === "string" && var2.length; // string
// parameters in function declaration
num = typeof param === "string" && param.length; // string
}
// Inside function declaration
private p1(param: string | number) {
// global vars in function declaration
num = typeof var1 === "string" && var1.length; // string
// variables in function declaration
var var2: string | number;
num = typeof var2 === "string" && var2.length; // string
// parameters in function declaration
num = typeof param === "string" && param.length; // string
}
// Inside function declaration
p2(param: string | number) {
// global vars in function declaration
num = typeof var1 === "string" && var1.length; // string
// variables in function declaration
var var2: string | number;
num = typeof var2 === "string" && var2.length; // string
// parameters in function declaration
num = typeof param === "string" && param.length; // string
}
// Inside function declaration
private static s1(param: string | number) {
// global vars in function declaration
num = typeof var1 === "string" && var1.length; // string
// variables in function declaration
var var2: string | number;
num = typeof var2 === "string" && var2.length; // string
// parameters in function declaration
num = typeof param === "string" && param.length; // string
}
// Inside function declaration
static s2(param: string | number) {
// global vars in function declaration
num = typeof var1 === "string" && var1.length; // string
// variables in function declaration
var var2: string | number;
num = typeof var2 === "string" && var2.length; // string
// parameters in function declaration
num = typeof param === "string" && param.length; // string
}
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardsInConditionalExpression.ts | TypeScript | // In the true expression of a conditional expression,
// the type of a variable or parameter is narrowed by any type guard in the condition when true,
// provided the true expression contains no assignments to the variable or parameter.
// In the false expression of a conditional expression,
// the type of a variable or parameter is narrowed by any type guard in the condition when false,
// provided the false expression contains no assignments to the variable or parameter.
function foo(x: number | string) {
return typeof x === "string"
? x.length // string
: x++; // number
}
function foo2(x: number | string) {
return typeof x === "string"
? ((x = "hello") && x) // string
: x; // number
}
function foo3(x: number | string) {
return typeof x === "string"
? ((x = 10) && x) // number
: x; // number
}
function foo4(x: number | string) {
return typeof x === "string"
? x // string
: ((x = 10) && x); // number
}
function foo5(x: number | string) {
return typeof x === "string"
? x // string
: ((x = "hello") && x); // string
}
function foo6(x: number | string) {
// Modify in both branches
return typeof x === "string"
? ((x = 10) && x) // number
: ((x = "hello") && x); // string
}
function foo7(x: number | string | boolean) {
return typeof x === "string"
? x === "hello" // boolean
: typeof x === "boolean"
? x // boolean
: x == 10; // boolean
}
function foo8(x: number | string | boolean) {
var b: number | boolean;
return typeof x === "string"
? x === "hello"
: ((b = x) && // number | boolean
(typeof x === "boolean"
? x // boolean
: x == 10)); // boolean
}
function foo9(x: number | string) {
var y = 10;
// usage of x or assignment to separate variable shouldn't cause narrowing of type to stop
return typeof x === "string"
? ((y = x.length) && x === "hello") // boolean
: x === 10; // boolean
}
function foo10(x: number | string | boolean) {
// Mixing typeguards
var b: boolean | number;
return typeof x === "string"
? x // string
: ((b = x) // x is number | boolean
&& typeof x === "number"
&& x.toString()); // x is number
}
function foo11(x: number | string | boolean) {
// Mixing typeguards
var b: number | boolean | string;
return typeof x === "string"
? x // string
: ((b = x) // x is number | boolean
&& typeof x === "number"
&& (x = 10) // assignment to x
&& x); // x is number
}
function foo12(x: number | string | boolean) {
// Mixing typeguards
var b: number | boolean | string;
return typeof x === "string"
? ((x = 10) && x.toString().length) // number
: ((b = x) // x is number | boolean
&& typeof x === "number"
&& x); // x is number
} | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardsInDoStatement.ts | TypeScript | let cond: boolean;
function a(x: string | number | boolean) {
x = true;
do {
x; // boolean | string
x = undefined;
} while (typeof x === "string")
x; // number | boolean
}
function b(x: string | number | boolean) {
x = true;
do {
x; // boolean | string
if (cond) continue;
x = undefined;
} while (typeof x === "string")
x; // number | boolean
}
function c(x: string | number) {
x = "";
do {
x; // string
if (cond) break;
x = undefined;
} while (typeof x === "string")
x; // string | number
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardsInExternalModule.ts | TypeScript | //@module: commonjs
// Note that type guards affect types of variables and parameters only and
// have no effect on members of objects such as properties.
// local variable in external module
var num: number;
var var1: string | number;
if (typeof var1 === "string") {
num = var1.length; // string
}
else {
num = var1; // number
}
// exported variable in external module
var strOrNum: string | number;
export var var2: string | number;
if (typeof var2 === "string") {
// export makes the var property and not variable
strOrNum = var2; // string | number
}
else {
strOrNum = var2; // number | string
} | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardsInForStatement.ts | TypeScript | let cond: boolean;
function a(x: string | number) {
for (x = undefined; typeof x !== "number"; x = undefined) {
x; // string
}
x; // number
}
function b(x: string | number) {
for (x = undefined; typeof x !== "number"; x = undefined) {
x; // string
if (cond) continue;
}
x; // number
}
function c(x: string | number) {
for (x = undefined; typeof x !== "number"; x = undefined) {
x; // string
if (cond) break;
}
x; // string | number
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardsInFunction.ts | TypeScript | // Note that type guards affect types of variables and parameters only and
// have no effect on members of objects such as properties.
// variables in global
var num: number;
var var1: string | number;
// Inside function declaration
function f(param: string | number) {
// global vars in function declaration
num = typeof var1 === "string" && var1.length; // string
// variables in function declaration
var var2: string | number;
num = typeof var2 === "string" && var2.length; // string
// parameters in function declaration
num = typeof param === "string" && param.length; // string
}
// local function declaration
function f1(param: string | number) {
var var2: string | number;
function f2(param1: string | number) {
// global vars in function declaration
num = typeof var1 === "string" && var1.length; // string
// variables from outer function declaration
num = typeof var2 === "string" && var2.length; // string
// parameters in outer declaration
num = typeof param === "string" && param.length; // string
// local
var var3: string | number;
num = typeof var3 === "string" && var3.length; // string
num = typeof param1 === "string" && param1.length; // string
}
}
// Function expression
function f2(param: string | number) {
// variables in function declaration
var var2: string | number;
// variables in function expressions
var r = function (param1: string | number) {
// global vars in function declaration
num = typeof var1 === "string" && var1.length; // string
// variables from outer function declaration
num = typeof var2 === "string" && var2.length; // string
// parameters in outer declaration
num = typeof param === "string" && param.length; // string
// local
var var3: string | number;
num = typeof var3 === "string" && var3.length; // string
num = typeof param1 === "string" && param1.length; // string
} (param);
}
// Arrow expression
function f3(param: string | number) {
// variables in function declaration
var var2: string | number;
// variables in function expressions
var r = ((param1: string | number) => {
// global vars in function declaration
num = typeof var1 === "string" && var1.length; // string
// variables from outer function declaration
num = typeof var2 === "string" && var2.length; // string
// parameters in outer declaration
num = typeof param === "string" && param.length; // string
// local
var var3: string | number;
num = typeof var3 === "string" && var3.length; // string
num = typeof param1 === "string" && param1.length; // string
})(param);
}
// Return type of function
// Inside function declaration
var strOrNum: string | number;
function f4() {
var var2: string | number = strOrNum;
return var2;
}
strOrNum = typeof f4() === "string" && f4(); // string | number | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardsInFunctionAndModuleBlock.ts | TypeScript | // typeguards are scoped in function/module block
function foo(x: number | string | boolean) {
return typeof x === "string"
? x
: function f() {
var b = x; // number | boolean
return typeof x === "boolean"
? x.toString() // boolean
: x.toString(); // number
} ();
}
function foo2(x: number | string | boolean) {
return typeof x === "string"
? x
: function f(a: number | boolean) {
var b = x; // new scope - number | boolean
return typeof x === "boolean"
? x.toString() // boolean
: x.toString(); // number
} (x); // x here is narrowed to number | boolean
}
function foo3(x: number | string | boolean) {
return typeof x === "string"
? x
: (() => {
var b = x; // new scope - number | boolean
return typeof x === "boolean"
? x.toString() // boolean
: x.toString(); // number
})();
}
function foo4(x: number | string | boolean) {
return typeof x === "string"
? x
: ((a: number | boolean) => {
var b = x; // new scope - number | boolean
return typeof x === "boolean"
? x.toString() // boolean
: x.toString(); // number
})(x); // x here is narrowed to number | boolean
}
// Type guards do not affect nested function declarations
function foo5(x: number | string | boolean) {
if (typeof x === "string") {
var y = x; // string;
function foo() {
var z = x; // string
}
}
}
module m {
var x: number | string | boolean;
module m2 {
var b = x; // new scope - number | boolean | string
var y: string;
if (typeof x === "string") {
y = x // string;
} else {
y = typeof x === "boolean"
? x.toString() // boolean
: x.toString(); // number
}
}
}
module m1 {
var x: number | string | boolean;
module m2.m3 {
var b = x; // new scope - number | boolean | string
var y: string;
if (typeof x === "string") {
y = x // string;
} else {
y = typeof x === "boolean"
? x.toString() // boolean
: x.toString(); // number
}
}
} | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardsInGlobal.ts | TypeScript | // Note that type guards affect types of variables and parameters only and
// have no effect on members of objects such as properties.
// variables in global
var num: number;
var var1: string | number;
if (typeof var1 === "string") {
num = var1.length; // string
}
else {
num = var1; // number
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardsInIfStatement.ts | TypeScript | // In the true branch statement of an 'if' statement,
// the type of a variable or parameter is narrowed by any type guard in the 'if' condition when true.
// In the false branch statement of an 'if' statement,
// the type of a variable or parameter is narrowed by any type guard in the 'if' condition when false.
function foo(x: number | string) {
if (typeof x === "string") {
return x.length; // string
}
else {
return x++; // number
}
}
function foo2(x: number | string) {
if (typeof x === "string") {
x = 10;
return x; // number
}
else {
return x; // number
}
}
function foo3(x: number | string) {
if (typeof x === "string") {
x = "Hello";
return x; // string
}
else {
return x; // number
}
}
function foo4(x: number | string) {
if (typeof x === "string") {
return x; // string
}
else {
x = 10;
return x; // number
}
}
function foo5(x: number | string) {
if (typeof x === "string") {
return x; // string
}
else {
x = "hello";
return x; // string
}
}
function foo6(x: number | string) {
if (typeof x === "string") {
x = 10;
return x; // number
}
else {
x = "hello";
return x; // string
}
}
function foo7(x: number | string | boolean) {
if (typeof x === "string") {
return x === "hello"; // string
}
else if (typeof x === "boolean") {
return x; // boolean
}
else {
return x == 10; // number
}
}
function foo8(x: number | string | boolean) {
if (typeof x === "string") {
return x === "hello"; // string
}
else {
var b: number | boolean = x; // number | boolean
if (typeof x === "boolean") {
return x; // boolean
}
else {
return x == 10; // number
}
}
}
function foo9(x: number | string) {
var y = 10;
if (typeof x === "string") {
// usage of x or assignment to separate variable shouldn't cause narrowing of type to stop
y = x.length;
return x === "hello"; // string
}
else {
return x == 10; // number
}
}
function foo10(x: number | string | boolean) {
// Mixing typeguard narrowing in if statement with conditional expression typeguard
if (typeof x === "string") {
return x === "hello"; // string
}
else {
var y: boolean | string;
var b = x; // number | boolean
return typeof x === "number"
? x === 10 // number
: x; // x should be boolean
}
}
function foo11(x: number | string | boolean) {
// Mixing typeguard narrowing in if statement with conditional expression typeguard
// Assigning value to x deep inside another guard stops narrowing of type too
if (typeof x === "string") {
return x; // string | number | boolean - x changed in else branch
}
else {
var y: number| boolean | string;
var b = x; // number | boolean | string - because below we are changing value of x in if statement
return typeof x === "number"
? (
// change value of x
x = 10 && x.toString() // number | boolean | string
)
: (
// do not change value
y = x && x.toString() // number | boolean | string
);
}
}
function foo12(x: number | string | boolean) {
// Mixing typeguard narrowing in if statement with conditional expression typeguard
// Assigning value to x in outer guard shouldn't stop narrowing in the inner expression
if (typeof x === "string") {
return x.toString(); // string | number | boolean - x changed in else branch
}
else {
x = 10;
var b = x; // number | boolean | string
return typeof x === "number"
? x.toString() // number
: x.toString(); // boolean | string
}
} | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardsInModule.ts | TypeScript | // Note that type guards affect types of variables and parameters only and
// have no effect on members of objects such as properties.
// variables in global
var num: number;
var strOrNum: string | number;
var var1: string | number;
// Inside module
module m1 {
// global vars in function declaration
num = typeof var1 === "string" && var1.length; // string
// variables in module declaration
var var2: string | number;
if (typeof var2 === "string") {
num = var2.length; // string
}
else {
num = var2; // number
}
// exported variable in the module
export var var3: string | number;
if (typeof var3 === "string") {
strOrNum = var3; // string | number
}
else {
strOrNum = var3; // string | number
}
}
// local module
module m2 {
var var2: string | number;
export var var3: string | number;
module m3 {
// global vars in function declaration
num = typeof var1 === "string" && var1.length; // string
// local variables from outer module declaration
num = typeof var2 === "string" && var2.length; // string
// exported variable from outer the module
strOrNum = typeof var3 === "string" && var3; // string | number
// variables in module declaration
var var4: string | number;
if (typeof var4 === "string") {
num = var4.length; // string
}
else {
num = var4; // number
}
// exported variable in the module
export var var5: string | number;
if (typeof var5 === "string") {
strOrNum = var5; // string | number
}
else {
strOrNum = var5; // string | number
}
}
}
// Dotted module
module m3.m4 {
// global vars in function declaration
num = typeof var1 === "string" && var1.length; // string
// variables in module declaration
var var2: string | number;
if (typeof var2 === "string") {
num = var2.length; // string
}
else {
num = var2; // number
}
// exported variable in the module
export var var3: string | number;
if (typeof var3 === "string") {
strOrNum = var3; // string | number
}
else {
strOrNum = var3; // string | number
}
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardsInProperties.ts | TypeScript | //@target: es5
// Note that type guards affect types of variables and parameters only and
// have no effect on members of objects such as properties.
var num: number;
var strOrNum: string | number;
class C1 {
private pp1: string | number;
pp2: string | number;
// Inside public accessor getter
get pp3() {
return strOrNum;
}
method() {
strOrNum = typeof this.pp1 === "string" && this.pp1; // string | number
strOrNum = typeof this.pp2 === "string" && this.pp2; // string | number
strOrNum = typeof this.pp3 === "string" && this.pp3; // string | number
}
}
var c1: C1;
strOrNum = typeof c1.pp2 === "string" && c1.pp2; // string | number
strOrNum = typeof c1.pp3 === "string" && c1.pp3; // string | number
var obj1: {
x: string | number;
};
strOrNum = typeof obj1.x === "string" && obj1.x; // string | number | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardsInRightOperandOfAndAndOperator.ts | TypeScript | // In the right operand of a && operation,
// the type of a variable or parameter is narrowed by any type guard in the left operand when true.
function foo(x: number | string) {
return typeof x === "string" && x.length === 10; // string
}
function foo2(x: number | string) {
// modify x in right hand operand
return typeof x === "string" && ((x = 10) && x); // string | number
}
function foo3(x: number | string) {
// modify x in right hand operand with string type itself
return typeof x === "string" && ((x = "hello") && x); // string | number
}
function foo4(x: number | string | boolean) {
return typeof x !== "string" // string | number | boolean
&& typeof x !== "number" // number | boolean
&& x; // boolean
}
function foo5(x: number | string | boolean) {
// usage of x or assignment to separate variable shouldn't cause narrowing of type to stop
var b: number | boolean;
return typeof x !== "string" // string | number | boolean
&& ((b = x) && (typeof x !== "number" // number | boolean
&& x)); // boolean
}
function foo6(x: number | string | boolean) {
// Mixing typeguard narrowing in if statement with conditional expression typeguard
return typeof x !== "string" // string | number | boolean
&& (typeof x !== "number" // number | boolean
? x // boolean
: x === 10) // number
}
function foo7(x: number | string | boolean) {
var y: number| boolean | string;
var z: number| boolean | string;
// Mixing typeguard narrowing
return typeof x !== "string"
&& ((z = x) // number | boolean
&& (typeof x === "number"
// change value of x
? ((x = 10) && x.toString()) // x is number
// do not change value
: ((y = x) && x.toString()))); // x is boolean
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardsInRightOperandOfOrOrOperator.ts | TypeScript | // In the right operand of a || operation,
// the type of a variable or parameter is narrowed by any type guard in the left operand when false,
// provided the right operand contains no assignments to the variable or parameter.
function foo(x: number | string) {
return typeof x !== "string" || x.length === 10; // string
}
function foo2(x: number | string) {
// modify x in right hand operand
return typeof x !== "string" || ((x = 10) || x); // string | number
}
function foo3(x: number | string) {
// modify x in right hand operand with string type itself
return typeof x !== "string" || ((x = "hello") || x); // string | number
}
function foo4(x: number | string | boolean) {
return typeof x === "string" // string | number | boolean
|| typeof x === "number" // number | boolean
|| x; // boolean
}
function foo5(x: number | string | boolean) {
// usage of x or assignment to separate variable shouldn't cause narrowing of type to stop
var b: number | boolean;
return typeof x === "string" // string | number | boolean
|| ((b = x) || (typeof x === "number" // number | boolean
|| x)); // boolean
}
function foo6(x: number | string | boolean) {
// Mixing typeguard
return typeof x === "string" // string | number | boolean
|| (typeof x !== "number" // number | boolean
? x // boolean
: x === 10) // number
}
function foo7(x: number | string | boolean) {
var y: number| boolean | string;
var z: number| boolean | string;
// Mixing typeguard narrowing
return typeof x === "string"
|| ((z = x) // number | boolean
|| (typeof x === "number"
// change value of x
? ((x = 10) && x.toString()) // number | boolean | string
// do not change value
: ((y = x) && x.toString()))); // number | boolean | string
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardsInWhileStatement.ts | TypeScript | let cond: boolean;
function a(x: string | number) {
while (typeof x === "string") {
x; // string
x = undefined;
}
x; // number
}
function b(x: string | number) {
while (typeof x === "string") {
if (cond) continue;
x; // string
x = undefined;
}
x; // number
}
function c(x: string | number) {
while (typeof x === "string") {
if (cond) break;
x; // string
x = undefined;
}
x; // string | number
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardsNestedAssignments.ts | TypeScript | // @strictNullChecks: true
class Foo {
x: string;
}
declare function getFooOrNull(): Foo | null;
declare function getStringOrNumberOrNull(): string | number | null;
function f1() {
let foo: Foo | null;
if ((foo = getFooOrNull()) !== null) {
foo; // Foo
}
}
function f2() {
let foo1: Foo | null;
let foo2: Foo | null;
if ((foo1 = getFooOrNull(), foo2 = foo1) !== null) {
foo1; // Foo | null
foo2; // Foo
}
}
function f3() {
let obj: Object | null;
if ((obj = getFooOrNull()) instanceof Foo) {
obj;
}
}
function f4() {
let x: string | number | null;
if (typeof (x = getStringOrNumberOrNull()) === "number") {
x;
}
}
// Repro from #8851
const re = /./g
let match: RegExpExecArray | null
while ((match = re.exec("xxx")) != null) {
const length = match[1].length + match[2].length
} | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardsObjectMethods.ts | TypeScript | //@target: es5
// Note that type guards affect types of variables and parameters only and
// have no effect on members of objects such as properties.
// variables in global
var num: number;
var strOrNum: string | number;
var var1: string | number;
var obj1 = {
// Inside method
method(param: string | number) {
// global vars in function declaration
num = typeof var1 === "string" && var1.length; // string
// variables in function declaration
var var2: string | number;
num = typeof var2 === "string" && var2.length; // string
// parameters in function declaration
num = typeof param === "string" && param.length; // string
return strOrNum;
},
get prop() {
// global vars in function declaration
num = typeof var1 === "string" && var1.length; // string
// variables in function declaration
var var2: string | number;
num = typeof var2 === "string" && var2.length; // string
return strOrNum;
},
set prop(param: string | number) {
// global vars in function declaration
num = typeof var1 === "string" && var1.length; // string
// variables in function declaration
var var2: string | number;
num = typeof var2 === "string" && var2.length; // string
// parameters in function declaration
num = typeof param === "string" && param.length; // string
}
};
// return expression of the method
strOrNum = typeof obj1.method(strOrNum) === "string" && obj1.method(strOrNum);
// accessing getter property
strOrNum = typeof obj1.prop === "string" && obj1.prop; | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardsOnClassProperty.ts | TypeScript | // Note that type guards affect types of variables and parameters only and
// have no effect on members of objects such as properties.
// Note that the class's property must be copied to a local variable for
// the type guard to have an effect
class D {
data: string | string[];
getData() {
var data = this.data;
return typeof data === "string" ? data : data.join(" ");
}
getData1() {
return typeof this.data === "string" ? this.data : this.data.join(" ");
}
}
var o: {
prop1: number|string;
prop2: boolean|string;
} = {
prop1: "string" ,
prop2: true
}
if (typeof o.prop1 === "string" && o.prop1.toLowerCase()) {}
var prop1 = o.prop1;
if (typeof prop1 === "string" && prop1.toLocaleLowerCase()) { } | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardsTypeParameters.ts | TypeScript | // @strictNullChecks: true
// Type guards involving type parameters produce intersection types
class C {
prop: string;
}
function f1<T>(x: T) {
if (x instanceof C) {
let v1: T = x;
let v2: C = x;
x.prop;
}
}
function f2<T>(x: T) {
if (typeof x === "string") {
let v1: T = x;
let v2: string = x;
x.length;
}
}
// Repro from #13872
function fun<T>(item: { [P in keyof T]: T[P] }) {
const strings: string[] = [];
for (const key in item) {
const value = item[key];
if (typeof value === "string") {
strings.push(value);
}
}
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardsWithAny.ts | TypeScript | var x: any = { p: 0 };
if (x instanceof Object) {
x.p; // No error, type any unaffected by instanceof type guard
}
else {
x.p; // No error, type any unaffected by instanceof type guard
}
if (typeof x === "string") {
x.p; // Error, type any narrowed by primitive type check
}
else {
x.p; // No error, type unaffected in this branch
}
if (typeof x === "number") {
x.p; // Error, type any narrowed by primitive type check
}
else {
x.p; // No error, type unaffected in this branch
}
if (typeof x === "boolean") {
x.p; // Error, type any narrowed by primitive type check
}
else {
x.p; // No error, type unaffected in this branch
}
if (typeof x === "object") {
x.p; // No error, type any only affected by primitive type check
}
else {
x.p; // No error, type unaffected in this branch
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardsWithInstanceOf.ts | TypeScript | // @strictNullChecks: true
interface I { global: string; }
var result!: I;
var result2!: I;
if (!(result instanceof RegExp)) {
result = result2;
} else if (!result.global) {
}
// Repro from #31155
interface OnChanges {
onChanges(changes: Record<string, unknown>): void
}
interface Validator {
validate(): null | Record<string, unknown>;
}
class C {
validate() {
return {}
}
}
function foo() {
let v: Validator & Partial<OnChanges> = null as any;
if (v instanceof C) {
v // Validator & Partial<OnChanges> & C
}
v // Validator & Partial<OnChanges> via subtype reduction
// In 4.1, we introduced a change which _fixed_ a bug with CFA
// correctly setting this to be the right object. With 4.2,
// we reverted that fix in #42231 which brought behavior back to
// before 4.1.
if (v.onChanges) {
v.onChanges({});
}
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardsWithInstanceOfByConstructorSignature.ts | TypeScript | interface AConstructor {
new (): A;
}
interface A {
foo: string;
}
declare var A: AConstructor;
var obj1: A | string;
if (obj1 instanceof A) { // narrowed to A.
obj1.foo;
obj1.bar;
}
var obj2: any;
if (obj2 instanceof A) {
obj2.foo;
obj2.bar;
}
// a construct signature with generics
interface BConstructor {
new <T>(): B<T>;
}
interface B<T> {
foo: T;
}
declare var B: BConstructor;
var obj3: B<number> | string;
if (obj3 instanceof B) { // narrowed to B<number>.
obj3.foo = 1;
obj3.foo = "str";
obj3.bar = "str";
}
var obj4: any;
if (obj4 instanceof B) {
obj4.foo = "str";
obj4.foo = 1;
obj4.bar = "str";
}
// has multiple construct signature
interface CConstructor {
new (value: string): C1;
new (value: number): C2;
}
interface C1 {
foo: string;
c: string;
bar1: number;
}
interface C2 {
foo: string;
c: string;
bar2: number;
}
declare var C: CConstructor;
var obj5: C1 | A;
if (obj5 instanceof C) { // narrowed to C1.
obj5.foo;
obj5.c;
obj5.bar1;
obj5.bar2;
}
var obj6: any;
if (obj6 instanceof C) {
obj6.foo;
obj6.bar1;
obj6.bar2;
}
// with object type literal
interface D {
foo: string;
}
declare var D: { new (): D; };
var obj7: D | string;
if (obj7 instanceof D) { // narrowed to D.
obj7.foo;
obj7.bar;
}
var obj8: any;
if (obj8 instanceof D) {
obj8.foo;
obj8.bar;
}
// a construct signature that returns a union type
interface EConstructor {
new (): E1 | E2;
}
interface E1 {
foo: string;
bar1: number;
}
interface E2 {
foo: string;
bar2: number;
}
declare var E: EConstructor;
var obj9: E1 | A;
if (obj9 instanceof E) { // narrowed to E1
obj9.foo;
obj9.bar1;
obj9.bar2;
}
var obj10: any;
if (obj10 instanceof E) {
obj10.foo;
obj10.bar1;
obj10.bar2;
}
// a construct signature that returns any
interface FConstructor {
new (): any;
}
interface F {
foo: string;
bar: number;
}
declare var F: FConstructor;
var obj11: F | string;
if (obj11 instanceof F) { // can't type narrowing, construct signature returns any.
obj11.foo;
obj11.bar;
}
var obj12: any;
if (obj12 instanceof F) {
obj12.foo;
obj12.bar;
}
// a type with a prototype, it overrides the construct signature
interface GConstructor {
prototype: G1; // high priority
new (): G2; // low priority
}
interface G1 {
foo1: number;
}
interface G2 {
foo2: boolean;
}
declare var G: GConstructor;
var obj13: G1 | G2;
if (obj13 instanceof G) { // narrowed to G1. G1 is return type of prototype property.
obj13.foo1;
obj13.foo2;
}
var obj14: any;
if (obj14 instanceof G) {
obj14.foo1;
obj14.foo2;
}
// a type with a prototype that has any type
interface HConstructor {
prototype: any; // high priority, but any type is ignored. interface has implicit `prototype: any`.
new (): H; // low priority
}
interface H {
foo: number;
}
declare var H: HConstructor;
var obj15: H | string;
if (obj15 instanceof H) { // narrowed to H.
obj15.foo;
obj15.bar;
}
var obj16: any;
if (obj16 instanceof H) {
obj16.foo1;
obj16.foo2;
}
var obj17: any;
if (obj17 instanceof Object) { // can't narrow type from 'any' to 'Object'
obj17.foo1;
obj17.foo2;
}
var obj18: any;
if (obj18 instanceof Function) { // can't narrow type from 'any' to 'Function'
obj18.foo1;
obj18.foo2;
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeGuardsWithInstanceOfBySymbolHasInstance.ts | TypeScript | // @lib: esnext
interface AConstructor {
new (): A;
[Symbol.hasInstance](value: unknown): value is A;
}
interface A {
foo: string;
}
declare var A: AConstructor;
var obj1: A | string;
if (obj1 instanceof A) { // narrowed to A.
obj1.foo;
obj1.bar;
}
var obj2: any;
if (obj2 instanceof A) {
obj2.foo;
obj2.bar;
}
// a construct signature with generics
interface BConstructor {
new <T>(): B<T>;
[Symbol.hasInstance](value: unknown): value is B<any>;
}
interface B<T> {
foo: T;
}
declare var B: BConstructor;
var obj3: B<number> | string;
if (obj3 instanceof B) { // narrowed to B<number>.
obj3.foo = 1;
obj3.foo = "str";
obj3.bar = "str";
}
var obj4: any;
if (obj4 instanceof B) {
obj4.foo = "str";
obj4.foo = 1;
obj4.bar = "str";
}
// has multiple construct signature
interface CConstructor {
new (value: string): C1;
new (value: number): C2;
[Symbol.hasInstance](value: unknown): value is C1 | C2;
}
interface C1 {
foo: string;
c: string;
bar1: number;
}
interface C2 {
foo: string;
c: string;
bar2: number;
}
declare var C: CConstructor;
var obj5: C1 | A;
if (obj5 instanceof C) { // narrowed to C1.
obj5.foo;
obj5.c;
obj5.bar1;
obj5.bar2;
}
var obj6: any;
if (obj6 instanceof C) {
obj6.foo;
obj6.bar1;
obj6.bar2;
}
// with object type literal
interface D {
foo: string;
}
declare var D: {
new (): D;
[Symbol.hasInstance](value: unknown): value is D;
};
var obj7: D | string;
if (obj7 instanceof D) { // narrowed to D.
obj7.foo;
obj7.bar;
}
var obj8: any;
if (obj8 instanceof D) {
obj8.foo;
obj8.bar;
}
// a construct signature that returns a union type
interface EConstructor {
new (): E1 | E2;
[Symbol.hasInstance](value: unknown): value is E1 | E2;
}
interface E1 {
foo: string;
bar1: number;
}
interface E2 {
foo: string;
bar2: number;
}
declare var E: EConstructor;
var obj9: E1 | A;
if (obj9 instanceof E) { // narrowed to E1
obj9.foo;
obj9.bar1;
obj9.bar2;
}
var obj10: any;
if (obj10 instanceof E) {
obj10.foo;
obj10.bar1;
obj10.bar2;
}
// a construct signature that returns any
interface FConstructor {
new (): any;
[Symbol.hasInstance](value: unknown): value is any;
}
interface F {
foo: string;
bar: number;
}
declare var F: FConstructor;
var obj11: F | string;
if (obj11 instanceof F) { // can't type narrowing, construct signature returns any.
obj11.foo;
obj11.bar;
}
var obj12: any;
if (obj12 instanceof F) {
obj12.foo;
obj12.bar;
}
// a type with a prototype, it overrides the construct signature
interface GConstructor {
prototype: G1; // high priority
new (): G2; // low priority
[Symbol.hasInstance](value: unknown): value is G1; // overrides priority
}
interface G1 {
foo1: number;
}
interface G2 {
foo2: boolean;
}
declare var G: GConstructor;
var obj13: G1 | G2;
if (obj13 instanceof G) { // narrowed to G1. G1 is return type of prototype property.
obj13.foo1;
obj13.foo2;
}
var obj14: any;
if (obj14 instanceof G) {
obj14.foo1;
obj14.foo2;
}
// a type with a prototype that has any type
interface HConstructor {
prototype: any; // high priority, but any type is ignored. interface has implicit `prototype: any`.
new (): H; // low priority
[Symbol.hasInstance](value: unknown): value is H; // overrides priority
}
interface H {
foo: number;
}
declare var H: HConstructor;
var obj15: H | string;
if (obj15 instanceof H) { // narrowed to H.
obj15.foo;
obj15.bar;
}
var obj16: any;
if (obj16 instanceof H) {
obj16.foo1;
obj16.foo2;
}
var obj17: any;
if (obj17 instanceof Object) { // can't narrow type from 'any' to 'Object'
obj17.foo1;
obj17.foo2;
}
var obj18: any;
if (obj18 instanceof Function) { // can't narrow type from 'any' to 'Function'
obj18.foo1;
obj18.foo2;
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeInferenceWithTupleType.ts | TypeScript | function combine<T, U>(x: T, y: U): [T, U] {
return [x, y];
}
var combineResult = combine("string", 10);
var combineEle1 = combineResult[0]; // string
var combineEle2 = combineResult[1]; // number
function zip<T, U>(array1: T[], array2: U[]): [[T, U]] {
if (array1.length != array2.length) {
return [[undefined, undefined]];
}
var length = array1.length;
var zipResult: [[T, U]];
for (var i = 0; i < length; ++i) {
zipResult.push([array1[i], array2[i]]);
}
return zipResult;
}
var zipResult = zip(["foo", "bar"], [5, 6]);
var zipResultEle = zipResult[0]; // [string, number]
var zipResultEleEle = zipResult[0][0]; // string
// #33559 and #33752
declare function f1<T1, T2>(values: [T1[], T2[]]): T1;
declare function f2<T1, T2>(values: readonly [T1[], T2[]]): T1;
let expected: "a";
expected = f1(undefined as ["a"[], "b"[]]);
expected = f2(undefined as ["a"[], "b"[]]);
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeLookupInIIFE.ts | TypeScript | // @noEmit: true
// @allowJs: true
// @checkJs: true
// @noImplicitAny: true
// @Filename: a.js
// #22973
var ns = (function() {})();
/** @type {ns.NotFound} */
var crash;
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeOfThisGeneral.ts | TypeScript | // @target: esnext
// @useDefineForClassFields: false
class MyTestClass {
private canary: number;
static staticCanary: number;
constructor() {
//type of 'this' in constructor body is the class instance type
var p = this.canary;
var p: number;
this.canary = 3;
}
//type of 'this' in member function param list is the class instance type
memberFunc(t = this) {
var t: MyTestClass;
//type of 'this' in member function body is the class instance type
var p = this;
var p: MyTestClass;
}
//type of 'this' in member accessor(get and set) body is the class instance type
get prop() {
var p = this;
var p: MyTestClass;
return this;
}
set prop(v) {
var p = this;
var p: MyTestClass;
p = v;
v = p;
}
someFunc = () => {
//type of 'this' in member variable initializer is the class instance type
var t = this;
var t: MyTestClass;
};
//type of 'this' in static function param list is constructor function type
static staticFn(t = this) {
var t: typeof MyTestClass;
var t = MyTestClass;
t.staticCanary;
//type of 'this' in static function body is constructor function type
var p = this;
var p: typeof MyTestClass;
var p = MyTestClass;
p.staticCanary;
}
static get staticProp() {
//type of 'this' in static accessor body is constructor function type
var p = this;
var p: typeof MyTestClass;
var p = MyTestClass;
p.staticCanary;
return this;
}
static set staticProp(v: typeof MyTestClass) {
//type of 'this' in static accessor body is constructor function type
var p = this;
var p: typeof MyTestClass;
var p = MyTestClass;
p.staticCanary;
}
}
class MyGenericTestClass<T, U> {
private canary: number;
static staticCanary: number;
constructor() {
//type of 'this' in constructor body is the class instance type
var p = this.canary;
var p: number;
this.canary = 3;
}
//type of 'this' in member function param list is the class instance type
memberFunc(t = this) {
var t: MyGenericTestClass<T, U>;
//type of 'this' in member function body is the class instance type
var p = this;
var p: MyGenericTestClass<T, U>;
}
//type of 'this' in member accessor(get and set) body is the class instance type
get prop() {
var p = this;
var p: MyGenericTestClass<T, U>;
return this;
}
set prop(v) {
var p = this;
var p: MyGenericTestClass<T, U>;
p = v;
v = p;
}
someFunc = () => {
//type of 'this' in member variable initializer is the class instance type
var t = this;
var t: MyGenericTestClass<T, U>;
};
//type of 'this' in static function param list is constructor function type
static staticFn(t = this) {
var t: typeof MyGenericTestClass;
var t = MyGenericTestClass;
t.staticCanary;
//type of 'this' in static function body is constructor function type
var p = this;
var p: typeof MyGenericTestClass;
var p = MyGenericTestClass;
p.staticCanary;
}
static get staticProp() {
//type of 'this' in static accessor body is constructor function type
var p = this;
var p: typeof MyGenericTestClass;
var p = MyGenericTestClass;
p.staticCanary;
return this;
}
static set staticProp(v: typeof MyGenericTestClass) {
//type of 'this' in static accessor body is constructor function type
var p = this;
var p: typeof MyGenericTestClass;
var p = MyGenericTestClass;
p.staticCanary;
}
}
//type of 'this' in a function declaration param list is Any
function fn(s = this) {
var s: any;
s.spaaaaaaace = 4;
//type of 'this' in a function declaration body is Any
var t: any;
var t = this;
this.spaaaaace = 4;
}
//type of 'this' in a function expression param list list is Any
var q1 = function (s = this) {
var s: any;
s.spaaaaaaace = 4;
//type of 'this' in a function expression body is Any
var t: any;
var t = this;
this.spaaaaace = 4;
}
//type of 'this' in a fat arrow expression param list is typeof globalThis
var q2 = (s = this) => {
var s: typeof globalThis;
s.spaaaaaaace = 4;
//type of 'this' in a fat arrow expression body is typeof globalThis
var t: typeof globalThis;
var t = this;
this.spaaaaace = 4;
}
//type of 'this' in global module is GlobalThis
var t: typeof globalThis;
var t = this;
this.spaaaaace = 4;
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeOfThisInAccessor.ts | TypeScript | class C {
get x() {
var r = this; // C
return 1;
}
static get y() {
var r2 = this; // typeof C
return 1;
}
}
class D<T> {
a: T;
get x() {
var r = this; // D<T>
return 1;
}
static get y() {
var r2 = this; // typeof D
return 1;
}
}
var x = {
get a() {
var r3 = this; // any
return 1;
}
} | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeOfThisInConstructorParamList.ts | TypeScript | //type of 'this' in constructor param list is the class instance type (error)
class ErrClass {
// Should be an error
constructor(f = this) { }
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeOfThisInFunctionExpression.ts | TypeScript | // type of 'this' in FunctionExpression is Any
function fn() {
var p = this;
var p: any;
}
var t = function () {
var p = this;
var p: any;
}
var t2 = function f() {
var x = this;
var x: any;
}
class C {
x = function () {
var q: any;
var q = this;
}
y = function ff() {
var q: any;
var q = this;
}
}
module M {
function fn() {
var p = this;
var p: any;
}
var t = function () {
var p = this;
var p: any;
}
var t2 = function f() {
var x = this;
var x: any;
}
} | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeOfThisInInstanceMember.ts | TypeScript | class C {
x = this;
foo() {
return this;
}
constructor(x: number) {
var t = this;
t.x;
t.y;
t.z;
var r = t.foo();
}
get y() {
return this;
}
}
var c: C;
// all ok
var r = c.x;
var ra = c.x.x.x;
var r2 = c.y;
var r3 = c.foo();
var rs = [r, r2, r3];
rs.forEach(x => {
x.foo;
x.x;
x.y;
}); | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeOfThisInInstanceMember2.ts | TypeScript | class C<T> {
x = this;
foo() {
return this;
}
constructor(x: T) {
var t = this;
t.x;
t.y;
t.z;
var r = t.foo();
}
get y() {
return this;
}
z: T;
}
var c: C<string>;
// all ok
var r = c.x;
var ra = c.x.x.x;
var r2 = c.y;
var r3 = c.foo();
var r4 = c.z;
var rs = [r, r2, r3];
rs.forEach(x => {
x.foo;
x.x;
x.y;
x.z;
}); | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeOfThisInMemberFunctions.ts | TypeScript | class C {
foo() {
var r = this;
}
static bar() {
var r2 = this;
}
}
class D<T> {
x: T;
foo() {
var r = this;
}
static bar() {
var r2 = this;
}
}
class E<T extends Date> {
x: T;
foo() {
var r = this;
}
static bar() {
var r2 = this;
}
} | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeOfThisInStaticMembers.ts | TypeScript | class C {
constructor(x: number) { }
static foo: number;
static bar() {
// type of this is the constructor function type
var t = this;
return this;
}
}
var t = C.bar();
// all ok
var r2 = t.foo + 1;
var r3 = t.bar();
var r4 = new t(1);
class C2<T> {
static test: number;
constructor(x: string) { }
static foo: string;
static bar() {
// type of this is the constructor function type
var t = this;
return this;
}
}
var t2 = C2.bar();
// all ok
var r5 = t2.foo + 1;
var r6 = t2.bar();
var r7 = new t2('');
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeOfThisInStaticMembers10.ts | TypeScript | // @target: esnext, es2022, es6, es5
// @experimentalDecorators: true
// @useDefineForClassFields: false
declare const foo: any;
@foo
class C {
static a = 1;
static b = this.a + 1;
}
@foo
class D extends C {
static c = 2;
static d = this.c + 1;
static e = super.a + this.c + 1;
static f = () => this.c + 1;
static ff = function () { this.c + 1 }
static foo () {
return this.c + 1;
}
static get fa () {
return this.c + 1;
}
static set fa (v: number) {
this.c = v + 1;
}
}
class CC {
static a = 1;
static b = this.a + 1;
}
class DD extends CC {
static c = 2;
static d = this.c + 1;
static e = super.a + this.c + 1;
static f = () => this.c + 1;
static ff = function () { this.c + 1 }
static foo () {
return this.c + 1;
}
static get fa () {
return this.c + 1;
}
static set fa (v: number) {
this.c = v + 1;
}
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeOfThisInStaticMembers11.ts | TypeScript | // @target: esnext, es2022, es6, es5
// @experimentalDecorators: true
// @useDefineForClassFields: true
declare const foo: any;
@foo
class C {
static a = 1;
static b = this.a + 1;
}
@foo
class D extends C {
static c = 2;
static d = this.c + 1;
static e = super.a + this.c + 1;
static f = () => this.c + 1;
static ff = function () { this.c + 1 }
static foo () {
return this.c + 1;
}
static get fa () {
return this.c + 1;
}
static set fa (v: number) {
this.c = v + 1;
}
}
class CC {
static a = 1;
static b = this.a + 1;
}
class DD extends CC {
static c = 2;
static d = this.c + 1;
static e = super.a + this.c + 1;
static f = () => this.c + 1;
static ff = function () { this.c + 1 }
static foo () {
return this.c + 1;
}
static get fa () {
return this.c + 1;
}
static set fa (v: number) {
this.c = v + 1;
}
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeOfThisInStaticMembers2.ts | TypeScript | class C {
static foo = this; // ok
}
class C2<T> {
static foo = this; // ok
} | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeOfThisInStaticMembers3.ts | TypeScript | // @target: esnext, es2022, es6, es5
// @useDefineForClassFields: false
class C {
static a = 1;
static b = this.a + 1;
}
class D extends C {
static c = 2;
static d = this.c + 1;
static e = super.a + this.c + 1;
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeOfThisInStaticMembers4.ts | TypeScript | // @target: esnext, es2022, es6, es5
// @useDefineForClassFields: true
class C {
static a = 1;
static b = this.a + 1;
}
class D extends C {
static c = 2;
static d = this.c + 1;
static e = super.a + this.c + 1;
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeOfThisInStaticMembers5.ts | TypeScript | // @target: esnext, es2022, es6, es5
class C {
static create = () => new this("yep")
constructor (private foo: string) {
}
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeOfThisInStaticMembers6.ts | TypeScript | class C {
static f = 1
}
class D extends C {
static c = super();
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeOfThisInStaticMembers7.ts | TypeScript | // @target: esnext, es2022, es6, es5
class C {
static a = 1;
static b = this.a + 1;
}
class D extends C {
static c = 2;
static d = this.c + 1;
static e = 1 + (super.a) + (this.c + 1) + 1;
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeOfThisInStaticMembers8.ts | TypeScript | // @target: esnext, es2022, es6, es5
class C {
static f = 1;
static arrowFunctionBoundary = () => this.f + 1;
static functionExprBoundary = function () { return this.f + 2 };
static classExprBoundary = class { a = this.f + 3 };
static functionAndClassDeclBoundary = (() => {
function foo () {
return this.f + 4
}
class CC {
a = this.f + 5
method () {
return this.f + 6
}
}
})();
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeOfThisInStaticMembers9.ts | TypeScript | // @target: esnext, es2022, es6, es5
class C {
static f = 1
}
class D extends C {
static arrowFunctionBoundary = () => super.f + 1;
static functionExprBoundary = function () { return super.f + 2 };
static classExprBoundary = class { a = super.f + 3 };
static functionAndClassDeclBoundary = (() => {
function foo () {
return super.f + 4
}
class C {
a = super.f + 5
method () {
return super.f +6
}
}
})();
}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeOfThisInstanceMemberNarrowedWithLoopAntecedent.ts | TypeScript | // #31995
type State = {
type: "numberVariant";
data: number;
} | {
type: "stringVariant";
data: string;
};
class SomeClass {
state!: State;
method() {
while (0) { }
this.state.data;
if (this.state.type === "stringVariant") {
const s: string = this.state.data;
}
}
}
class SomeClass2 {
state!: State;
method() {
const c = false;
while (c) { }
if (this.state.type === "numberVariant") {
this.state.data;
}
let n: number = this.state?.data; // This should be an error
}
} | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeOnlyMerge1.ts | TypeScript | // @Filename: a.ts
interface A {}
export type { A };
// @Filename: b.ts
import { A } from "./a";
const A = 0;
export { A };
// @Filename: c.ts
import { A } from "./b";
A;
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeOnlyMerge2.ts | TypeScript | // @Filename: a.ts
const A = {}
export { A };
// @Filename: b.ts
import { A } from "./a";
type A = any;
export type { A };
// @Filename: c.ts
import { A } from "./b";
namespace A {}
export { A };
// @Filename: d.ts
import { A } from "./c";
A;
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeOnlyMerge3.ts | TypeScript | // @Filename: a.ts
function A() {}
export type { A };
// @Filename: b.ts
import { A } from "./a";
namespace A {
export const displayName = "A";
}
export { A };
// @Filename: c.ts
import { A } from "./b";
A;
A.displayName;
A();
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeParameterAsBaseType.ts | TypeScript | // type parameters cannot be used as base types
// these are all errors
class C<T> extends T { }
class C2<T, U> extends U { }
interface I<T> extends T { }
interface I2<T, U> extends U { }
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeParameterAsTypeArgument.ts | TypeScript | // These are all errors because type parameters cannot reference other type parameters from the same list
function foo<T, U>(x: T, y: U) {
foo<U, U>(y, y);
return new C<U,T>();
}
class C<T, U> {
x: T;
}
interface I<T, U> {
x: C<U, T>;
}
//function foo<T, U extends T>(x: T, y: U) {
// foo<U, U>(y, y);
// return new C<U, T>();
//}
//class C<T extends U, U> {
// x: T;
//}
//interface I<T, U extends T> {
// x: C<U, T>;
//}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeParameterAsTypeParameterConstraint.ts | TypeScript | // using a type parameter as a constraint for a type parameter is valid
// no errors expected except illegal constraints
function foo<T, U extends T>(x: T, y: U): U { return y; }
var r = foo(1, 2);
var r = foo({}, 1);
interface A {
foo: string;
}
interface B extends A {
bar: number;
}
var a: A;
var b: B;
var r2 = foo(a, b);
var r3 = foo({ x: 1 }, { x: 2, y: 3 });
function foo2<T, U extends { length: T }>(x: T, y: U) { return y; }
foo2(1, '');
foo2({}, { length: 2 });
foo2(1, { width: 3, length: 2 });
foo2(1, []);
foo2(1, ['']); | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeParameterAsTypeParameterConstraint2.ts | TypeScript | // using a type parameter as a constraint for a type parameter is invalid
// these should be errors unless otherwise noted
function foo<T, U extends T>(x: T, y: U): U { return y; } // this is now an error
foo(1, '');
foo(1, {});
interface NumberVariant extends Number {
x: number;
}
var n: NumberVariant;
var r3 = foo(1, n);
function foo2<T, U extends { length: T }>(x: T, y: U) { return y; } // this is now an error
foo2(1, { length: '' });
foo2(1, { length: {} });
foo2([], ['']); | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeParameterAsTypeParameterConstraintTransitively.ts | TypeScript | // using a type parameter as a constraint for a type parameter is valid
// no errors expected
interface A { foo: number }
interface B extends A { bar: string; }
interface C extends B { baz: boolean; }
var a: A;
var b: B;
var c: C;
function foo<T, U, V>(x: T, y: U, z: V): V { return z; }
//function foo<T, U extends T, V extends U>(x: T, y: U, z: V): V { return z; }
foo(1, 2, 3);
foo({ x: 1 }, { x: 1, y: '' }, { x: 2, y: '', z: true });
foo(a, b, c);
foo(a, b, { foo: 1, bar: '', hm: true });
foo((x: number, y) => { }, (x) => { }, () => { });
function foo2<T extends A, U, V>(x: T, y: U, z: V): V { return z; }
//function foo2<T extends A, U extends T, V extends U>(x: T, y: U, z: V): V { return z; }
foo(a, a, a);
foo(a, b, c);
foo(b, b, { foo: 1, bar: '', hm: '' }); | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeParameterAsTypeParameterConstraintTransitively2.ts | TypeScript | // using a type parameter as a constraint for a type parameter is invalid
// these should be errors at the type parameter constraint declarations, and have no downstream errors
interface A { foo: number }
interface B extends A { bar: string; }
interface C extends B { baz: boolean; }
var a: A;
var b: B;
var c: C;
function foo<T, U, V>(x: T, y: U, z: V): V { return z; }
//function foo<T, U extends T, V extends U>(x: T, y: U, z: V): V { return z; }
foo(1, 2, '');
foo({ x: 1 }, { x: 1, y: '' }, { x: 2, y: 2, z: true });
foo(a, b, a);
foo(a, { foo: 1, bar: '', hm: true }, b);
foo((x: number, y: string) => { }, (x, y: boolean) => { }, () => { });
function foo2<T extends A, U extends A, V extends A>(x: T, y: U, z: V): V { return z; }
//function foo2<T extends A, U extends T, V extends U>(x: T, y: U, z: V): V { return z; }
foo(b, a, c);
foo(c, c, a); | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeParameterAssignability.ts | TypeScript | // type parameters are not assignable to one another unless directly or indirectly constrained to one another
function foo<T, U>(t: T, u: U) {
t = u; // error
u = t; // error
} | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeParameterAssignability2.ts | TypeScript | // type parameters are not assignable to one another unless directly or indirectly constrained to one another
function foo<T, U extends T>(t: T, u: U) {
t = u; // error
u = t; // ok
}
function foo2<T extends U, U>(t: T, u: U) {
t = u; // error
u = t; // ok
}
function foo3<T extends U, U extends V, V>(t: T, u: U, v: V) {
t = u; // error
u = t;
t = v; // error
v = t; // ok
u = v; // error
v = u; // ok
}
function foo4<T extends U, U extends V, V extends Date>(t: T, u: U, v: V) {
t = u; // error
t = v; // error
t = new Date(); // error
u = t;
u = v; // error
u = new Date(); // error
v = t;
v = u;
v = new Date(); // ok
var d: Date;
d = t; // ok
d = u; // ok
d = v; // ok
}
// same as foo4 with different type parameter ordering
function foo5<V extends Date, U extends V, T extends U>(t: T, u: U, v: V) {
t = u; // error
t = v; // error
t = new Date(); // error
u = t;
u = v; // error
u = new Date(); // error
v = t;
v = u;
v = new Date(); // ok
var d: Date;
d = t; // ok
d = u; // ok
d = v; // ok
}
function foo6<T extends U, U, V>(t: T, u: U, v: V) {
t = u; // error
t = v; // error
u = t; // ok
u = v; // error
v = t; // error
v = u; // error
} | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeParameterAssignability3.ts | TypeScript | // type parameters are not assignable to one another unless directly or indirectly constrained to one another
class Foo { foo: string; }
function foo<T extends Foo, U extends Foo>(t: T, u: U) {
var a: T;
var b: U;
t = a; // ok
a = t; // ok
b = u; // ok
u = b; // ok
t = u; // error
u = t; // error
}
class C<T extends Foo, U extends Foo> {
t: T;
u: U;
r = () => {
this.t = this.u; // error
this.u = this.t; // error
}
} | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeParameterConstModifiersReverseMappedTypes.ts | TypeScript | // @strict: true
// @noEmit: true
declare function test1<const T>(obj: {
[K in keyof T]: T[K];
}): [T, typeof obj];
const result1 = test1({
prop: "foo",
nested: {
nestedProp: "bar",
},
});
declare function test2<const T>(obj: {
readonly [K in keyof T]: T[K];
}): [T, typeof obj];
const result2 = test2({
prop: "foo",
nested: {
nestedProp: "bar",
},
});
declare function test3<const T>(obj: {
-readonly [K in keyof T]: T[K];
}): [T, typeof obj];
const result3 = test3({
prop: "foo",
nested: {
nestedProp: "bar",
},
});
declare function test4<const T extends readonly unknown[]>(arr: {
[K in keyof T]: T[K];
}): T;
const result4 = test4(["1", 2]);
declare function test5<const T extends readonly unknown[]>(
...args: {
[K in keyof T]: T[K];
}
): T;
const result5 = test5({ a: "foo" });
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeParameterConstModifiersWithIntersection.ts | TypeScript | // @strict: true
// @noEmit: true
// https://github.com/microsoft/TypeScript/issues/55778
interface Config<T1 extends { type: string }> {
useIt: T1;
}
declare function test<
T1 extends { type: string },
const TConfig extends Config<T1>,
>(config: { produceThing: T1 } & TConfig): TConfig;
const result = test({
produceThing: {} as {
type: "foo";
},
useIt: {
type: "foo",
},
extra: 10,
}); | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeParameterDirectlyConstrainedToItself.ts | TypeScript | // all of the below should be errors
class C<T extends T> { }
class C2<T, U extends U> { }
interface I<T extends T> { }
interface I2<T, U extends U> { }
function f<T extends T>() { }
function f2<T, U extends U>() { }
var a: {
<T extends T>(): void;
<T, U extends U>(): void;
}
var b = <T extends T>() => { }
var b2 = <T, U extends U>() => { } | willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeParameterExtendsUnionConstraintDistributed.ts | TypeScript | type A = 1 | 2;
function f<T extends A>(a: T): A & T { return a; } // Shouldn't error
type B = 2 | 3;
function f2<T extends A, U extends B>(ab: T & U): (A | B) & T & U { return ab; } // Also shouldn't error
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University | |
crates/swc_ecma_parser/tests/tsc/typeParameterIndirectlyConstrainedToItself.ts | TypeScript | class C<U extends T, T extends U> { }
class C2<T extends U, U extends V, V extends T> { }
interface I<U extends T, T extends U> { }
interface I2<T extends U, U extends V, V extends T> { }
function f<U extends T, T extends U>() { }
function f2<T extends U, U extends V, V extends T>() { }
var a: {
<U extends T, T extends U>(): void;
<T extends U, U extends V, V extends T>(): void;
}
var b = <U extends T, T extends U>() => { }
var b2 = <T extends U, U extends V, V extends T>() => { }
class D<U extends T, T extends V, V extends T> { }
// Repro from #25740
type Foo<T> = [T] extends [number] ? {} : {};
function foo<S extends Foo<S>>() {}
| willcrichton/ilc-swc | 1 | Rust | willcrichton | Will Crichton | Brown University |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.